gpt4 book ai didi

java - 解析并比较 XML 标签文本以获得同级

转载 作者:行者123 更新时间:2023-12-02 07:23:03 26 4
gpt4 key购买 nike

我正在解析 NOAA XML,如下所示:

<time-layout time-coordinate="local" summarization="12hourly">
<layout-key>k-p12h-n14-1</layout-key>
<start-valid-time period-name="Tonight">2012-12-18T18:00:00-08:00</start-valid-time>
<start-valid-time period-name="Wednesday">2012-12-19T06:00:00-08:00</start-valid-time>
<start-valid-time period-name="Wednesday Night">2012-12-19T18:00:00-08:00</start-valid-time>
<start-valid-time period-name="Thursday">2012-12-20T06:00:00-08:00</start-valid-time>
<start-valid-time period-name="Thursday Night">2012-12-20T18:00:00-08:00</start-valid-time>
<start-valid-time period-name="Friday">2012-12-21T06:00:00-08:00</start-valid-time>
<start-valid-time period-name="Friday Night">2012-12-21T18:00:00-08:00</start-valid-time>
<start-valid-time period-name="Saturday">2012-12-22T06:00:00-08:00</start-valid-time>
<start-valid-time period-name="Saturday Night">2012-12-22T18:00:00-08:00</start-valid-time>
<start-valid-time period-name="Sunday">2012-12-23T06:00:00-08:00</start-valid-time>
<start-valid-time period-name="Sunday Night">2012-12-23T18:00:00-08:00</start-valid-time>
<start-valid-time period-name="Monday">2012-12-24T06:00:00-08:00</start-valid-time>
<start-valid-time period-name="Monday Night">2012-12-24T18:00:00-08:00</start-valid-time>
<start-valid-time period-name="Christmas Day">2012-12-25T06:00:00-08:00</start-valid-time>
</time-layout>

文档中还有其他标签,但我想要 k-p12h-n14-1 标签。

如何获取 k-p12h-n14-1 后面的周期名称的 period-name="*"?

这是一个FULL XML FILE EXAMPLE

我尝试使用以下方法来解决这个问题:

List<XmlDom> wxday = xml.tags("time-layout","time-coordinate","summarization");
String wxd = wxday.child("layout-key");
String wxdd = wxday.equals("k-p12h-n14-1");
kind of lost....

感谢您的任何想法。

最佳答案

您可以使用以下代码:

    File fXmlFile = new File("MapClick.php.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

Element rootElement = doc.getDocumentElement();

NodeList nodeList = rootElement.getElementsByTagName("layout-key");
int size = nodeList.getLength();

for (int i = 0; i < size; i++) {
Node currentNode = nodeList.item(i);

if (currentNode.getTextContent().trim().equals("k-p12h-n14-1")) {
while (currentNode.getNextSibling() != null) {
currentNode = currentNode.getNextSibling();

if (currentNode.getNodeName().equals("start-valid-time")) {
System.out.println(((Element)currentNode).getAttribute("period-name"));
}
}
}
}

关于java - 解析并比较 XML 标签文本以获得同级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13946563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com