下面是我试图用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
答案 0 :(得分:1)
nList.item(0).getChildNodes().item(9).getChildNodes().item(1).getChildNodes().item(1).getTextContent()
- &gt;打印$1.00
和
nList.item(0).getChildNodes().item(9).getChildNodes().item(1).getChildNodes().item(3).getTextContent()
- &gt;打印2hr
。
此行nList
之后使用NodeList nList = doc.getElementsByTagName("Placemark");
。请相应地修正您的遍历。
答案 1 :(得分:0)
我不确定你到底想要什么。也许精心制作一下。
org.w3c.Node有一个方法getTextContent()
。通常,这些w3c类使用item(i)
的强制转换,例如Element。
要跳过空白文本(节点名称#text`),或者只是更直接地访问特定元素,可以使用XPath。