XML Parser返回#text而不是节点名称,我不知道它们来自哪里

时间:2017-08-30 10:42:06

标签: java xml

下面是我正在解析一些XML的代码片段,但是我无法弄清楚如何正确地向下移动DOM树。

现在当我运行我的代码时,我返回顶部节点(根节点?),它是DistanceResponseMatrix,它有9个子节点。

我正在尝试遍历子节点,对于所有rowResponseList.item(i).getNodeName()(9个节点)它返回的#text。我期待一个包含行,元素等的节点列表,它将与XML对齐。

显然我在某个地方出错了,非常感谢任何帮助。

Xml:

<DistanceMatrixResponse>
<status>OK</status>
<origin_address>97 London Rd, Manchester M1 2PG, UK</origin_address>
<destination_address>1 Aytoun St, Manchester M1 3DB, UK</destination_address>
 <row>
 <element>
  <status>OK</status>
   <duration>
    <value>230</value>
    <text>4 mins</text>
   </duration>
   <distance>
    <value>888</value>
    <text>0.9 km</text>
    </distance>
   <duration_in_traffic>
    <value>215</value>
    <text>4 mins</text>
   </duration_in_traffic>
  </element>
 </row>
</DistanceMatrixResponse>

Java代码:

public static void main(String[] args){

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();      
    //URL need to be broken out into variables
    try {
        URL url = new URL("https://maps.googleapis.com/maps/api/distancematrix/xml?origins=53.474073,-2.230146&destinations=53.480037,-2.235950&mode=driving&language=en-FR&departure_time=now&key=");
        URLConnection conn = url.openConnection();

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(conn.getInputStream());
        NodeList dmrResponseList = doc.getElementsByTagName("DistanceMatrixResponse");
        System.out.println(dmrResponseList.getLength());

        for(int i = 0 ;i<dmrResponseList.getLength();i++) {
            Node p1 = dmrResponseList.item(i);
        String dmrstr = p1.getNodeName();
        System.out.println(dmrstr); // print out the name

        //if the node is an element node (should be) 
            if(p1.getNodeType() ==Node.ELEMENT_NODE) {
                NodeList  rowResponseList  = p1.getChildNodes(); // dont forget to cast when you get to an elemt
                System.out.println(rowResponseList.getLength());

                String rowstr = rowResponseList.item(i).getNodeName();
                System.out.println(rowstr); // print out the name


                //String areaStr = area.getAttribute("qtd");
                for(int j = 0 ;j<p1.getChildNodes().getLength();j++ ) {
                    Node k = rowResponseList.item(j);
                    if(k.getNodeType()== Node.TEXT_NODE) {
                        //Element name = (Element) k;
                        //String nameStr = name.getAttribute("name");
                        //System.out.println(areaStr + nameStr);
                        System.out.println(rowResponseList.item(j).getNodeName());
                    }
                }                   
            }
        }       
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) { 
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}   

0 个答案:

没有答案