来自org.w3c.dom的getElementsByTagName没有获取所有子节点

时间:2016-02-17 07:13:19

标签: java xml

我想让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")和每行的里程数。

任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

根据你的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);
    }
相关问题