0

下面是我试图用 DOM、我的代码和我的输出来解析的 XML。我需要从“简单数据”中获取信息,但我没有这样做。

XML:

<kml>
  <Document>
    <Folder id="kml_ft_Meter_Rates_and_Time_Limits">
      <name>Meter_Rates_and_Time_Limits</name>
      <Placemark id="kml_1">
        <name>$1.00 / hr 2hr time limit</name>
        <snippet> </snippet>
        <description><![CDATA[<center><table><tr><th colspan='2' align='center'><em>Attributes</em></th></tr><tr bgcolor="#E3E3F3">
          <th>RATE</th>
          <td>$1.00</td>
          </tr><tr bgcolor="">
          <th>LIMIT</th>
          <td>2hr</td>
          </tr></table></center>]]>
        </description>
        <styleUrl>#ParkingMeterStyler_KMLStyler</styleUrl>
        <ExtendedData>
          <SchemaData schemaUrl="#Meter_Rates_and_Time_Limits">
            <SimpleData name="RATE">$1.00</SimpleData>
            <SimpleData name="LIMIT">2hr</SimpleData>
          </SchemaData>
        </ExtendedData>
        <LineString>
          <coordinates>-123.100739208611,49.2630169018194,0 -123.100348847572,49.2630078055425,0 </coordinates>
        </LineString>
      </Placemark>
    </Folder>
  </Document>
</kml>

出于调试目的,用 sysouts 填充的代码:

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        System.out.println("Root 1st child :" + doc.getDocumentElement().getChildNodes().item(1).getNodeName());
        System.out.println("Document 1st child :" + doc.getDocumentElement().getChildNodes().item(1).getChildNodes().item(1).getNodeName());
        System.out.println("Document 2nd child :" + doc.getDocumentElement().getChildNodes().item(1).getChildNodes().item(2).getNodeName());
        System.out.println("Document 3rd child :" + doc.getDocumentElement().getChildNodes().item(1).getChildNodes().item(3).getNodeName());
        System.out.println("Document 4th child :" + doc.getDocumentElement().getChildNodes().item(1).getChildNodes().item(4).getNodeName());
        System.out.println("Document 5th child :" + doc.getDocumentElement().getChildNodes().item(1).getChildNodes().item(5).getNodeName());
        System.out.println("-----------------------");


        NodeList nList = doc.getElementsByTagName("Placemark");
        nList = nList.item(1).getChildNodes();
        System.out.println("Placemark list, 1st placemark 1st child :" + nList.item(1).getNodeName());
        System.out.println("Placemark list, 1st placemark 2nd child :" + nList.item(2).getNodeName());
        System.out.println("Placemark list, 1st placemark 3rd child :" + nList.item(3).getNodeName());
        System.out.println("Placemark list, 1st placemark 4th child :" + nList.item(4).getNodeName());
        System.out.println("-----------------------");
        System.out.println("Placemark list, 1st placemark 9th child :" + nList.item(9).getNodeName());
        System.out.println("-----------------------");
        nList = nList.item(9).getChildNodes();
        System.out.println("Extended data, 1st child :" + nList.item(1).getNodeName());
        System.out.println("-----------------------");
        System.out.println("Schema data, 1st child :" + nList.item(1).getChildNodes().item(1).getNodeName());
        System.out.println("Simple data :" + nList.item(1).getChildNodes().item(4).getNodeName());
        System.out.println("-----------------------");
        System.out.println("Schema data, 2nd child :" + nList.item(1).getChildNodes().item(3).getNodeName());
        System.out.println("Simple data :" + nList.item(1).getChildNodes().item(4).getNodeName());

控制台输出:

Root element :kml
Root 1st child :Document
Document 1st child :name
Document 2nd child :#text
Document 3rd child :visibility
Document 4th child :#text
Document 5th child :Style
-----------------------
Placemark list, 1st placemark 1st child :name
Placemark list, 1st placemark 2nd child :#text
Placemark list, 1st placemark 3rd child :snippet
Placemark list, 1st placemark 4th child :#text
-----------------------
Placemark list, 1st placemark 9th child :ExtendedData
-----------------------
Extended data, 1st child :SchemaData
-----------------------
Schema data, 1st child :SimpleData
Simple data :#text
-----------------------
Schema data, 2nd child :SimpleData
Simple data :#text
4

2 回答 2

1

nList.item(0).getChildNodes().item(9).getChildNodes().item(1).getChildNodes().item(1).getTextContent()--> 打印$1.00

nList.item(0).getChildNodes().item(9).getChildNodes().item(1).getChildNodes().item(3).getTextContent()--> 打印2hr.

这里nList用在这行之后NodeList nList = doc.getElementsByTagName("Placemark");。请相应地修复您的遍历。

于 2012-10-20T21:30:14.770 回答
0

我不确定你到底想要什么。也许再详细一点。

org.w3c.Node 有一个方法getTextContent()。通常,这些 w3c 类使用 to 的强制转换,item(i)例如Element

要跳过空白文本(节点名称#text`),或者只是更直接地访问特定元素,可以使用 XPath。

于 2012-10-20T21:03:40.523 回答