[英]Understanding DOM Parser for Java
Below is the XML I'm trying to parse with DOM, my code and my output. 以下是我尝试与DOM,我的代码和我的输出解析的XML。 I need to get the information out of "Simple data" but I'm failing to do so.
我需要从“简单数据”中获取信息,但是我没有这样做。
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>
Code filled with sysouts for debugging purposes: 出于调试目的,用sysout填充的代码:
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());
Console output: 控制台输出:
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
nList.item(0).getChildNodes().item(9).getChildNodes().item(1).getChildNodes().item(1).getTextContent()
--> prints $1.00
and 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()
--> prints the 2hr
. nList.item(0).getChildNodes().item(9).getChildNodes().item(1).getChildNodes().item(3).getTextContent()
->显示2hr
。
Here nList
is used after this line NodeList nList = doc.getElementsByTagName("Placemark");
这里
nList
被这条线之后使用NodeList nList = doc.getElementsByTagName("Placemark");
. 。 Please fix your traversal accordingly.
请相应解决遍历。
I am not certain of what you exactly want. 我不确定您到底想要什么。 Maybe elaborate a bit more.
也许再详细一点。
org.w3c.Node has a method getTextContent()
. org.w3c.Node有一个
getTextContent()
方法。 In general these w3c classes use casts of item(i)
to for instance Element . 通常,这些w3c类将
item(i)
类型强制转换为Element 。
To skip whitespace Text (node name #text`), or simply more directly access specific elements, one uses XPath. 要跳过空白文本(节点名称#text`),或更简单地直接访问特定元素,可以使用XPath。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.