有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java XML解析器返回#文本而不是节点名,我不知道它们来自哪里

下面是我正在处理的一段代码,用于解析一些XML,但我无法确定如何正确地向下移动DOM树

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

我试图在子节点之间循环,以获取所有rowResponseList.item(i).getNodeName()(9个节点)返回的#文本。我期待一个包含行、元素等的节点列表,它将和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) 个答案