下面是我试图用 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