gpt4 book ai didi

java - 来自 org.w3c.dom 的 getElementsByTagName 未获取所有子节点

转载 作者:太空宇宙 更新时间:2023-11-04 12:55:15 25 4
gpt4 key购买 nike

我试图让 getElementByTagName("NAME").item(0).getTextContent() 将 xml 的所有子元素("CONNECTION""DISTANCE")打印到控制台。 item(0) 调用显然只打印 ArrayList 连接的第一个元素。

xml:

<ROW>    
<FACILITY>SEATTLE, WA</FACILITY>
<CONNECTIONS>
<CONNECTION>
<NAME>FARGO, ND</NAME>
<DISTANCE>1426</DISTANCE>
</CONNECTION>
<CONNECTION>
<NAME>SAN FRANCISCO, CA</NAME>
<DISTANCE>808</DISTANCE>
</CONNECTION>
</CONNECTIONS>
</ROW>
<ROW>
<FACILITY>SAN FRANCISCO, CA</FACILITY>
<CONNECTIONS>
<CONNECTION>
<NAME>SEATTLE, WA</NAME>
<DISTANCE>808</DISTANCE>
</CONNECTION>
<CONNECTION>
<NAME>DENVER, CO</NAME>
<DISTANCE>1249</DISTANCE>
</CONNECTION>
<CONNECTION>
<NAME>LOS ANGELES, CA</NAME>
<DISTANCE>382</DISTANCE>
</CONNECTION>
</CONNECTIONS>
</ROW>

java:

ArrayList<String> connections = new ArrayList<>();
NodeList connectionsList = elem.getElementsByTagName("CONNECTIONS");
for (int j = 0; j < connectionsList.getLength(); j++) {
if (connectionsList.item(j).getNodeType() == Node.TEXT_NODE) {
continue;
}

entryName = connectionsList.item(j).getNodeName();
if (!entryName.equals("CONNECTIONS")) {
System.err.println("Unexpected node found: " + entryName);
return;
}

// Get some named nodes
elem = (Element) connectionsList.item(j);
String connectionName = elem.getElementsByTagName("NAME").item(0).getTextContent();
String connectionDist = elem.getElementsByTagName("DISTANCE").item(0).getTextContent();
facilities.addConnection(facilityName, connectionName, (Integer.parseInt(connectionDist)));

// Create a string summary of the tags
connections.add(connectionName + ":" + connectionDist);
}

输出应如下所示:

Facility: SEATTLE, WA Direct Links: [FARGO, ND:1426, SAN FRANCISCO, CA:808]

Facility: SAN FRANCISCO, CA Direct Links: [SEATTLE, WA:808, DENVER, CO:1249, LOS ANGELES, CA:382]

实际输出仅打印第一个城市(“CONNECTION”)和每行的里程。

任何帮助表示赞赏。

最佳答案

根据您的 xml 结构,您需要迭代 CONNECTION 节点,并为每个节点打印 NAME 和 DISTANCE 文本内容。

    NodeList connectionsList = doc.getElementsByTagName("CONNECTIONS");
for (int j = 0; j < connectionsList.getLength(); j++) {
if (connectionsList.item(j).getNodeType() == Node.TEXT_NODE) {
continue;
}

String entryName = connectionsList.item(j).getNodeName();
if (!entryName.equals("CONNECTIONS")) {
System.err.println("Unexpected node found: " + entryName);
return;
}

// Get some named nodes
Node elem = (Element) connectionsList.item(j).getChildNodes(); // list of CONNECTION ELEMENTS
List<String> facilities = new ArrayList<String>();
for(int i = 0 ; i< elem.getChildNodes().getLength() ;i++){
Node subElem = elem.getChildNodes().item(i); //CONNECTION ELEMENT
String connectionName ="";
String connectionDist = "";
for(int k = 0 ; k< subElem.getChildNodes().getLength(); k++){
Node connectionElem = subElem.getChildNodes().item(k);
if("NAME".equals(connectionElem.getNodeName())){
connectionName = connectionElem.getTextContent();
}
else if("DISTANCE".equals(connectionElem.getNodeName())){
connectionDist = connectionElem.getTextContent();
}
if(!connectionDist.equals("") && !connectionName.equals("")) {
facilities.add(connectionName + " , " + connectionDist);
connectionDist ="";
connectionName = "";
}
}

}
System.out.println("Facility: "+doc.getElementsByTagName("FACILITY").item(j).getTextContent() +" Direct Links: "+facilities);
}

关于java - 来自 org.w3c.dom 的 getElementsByTagName 未获取所有子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35450154/

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