2012-10-20 112 views
0

下面是我想用DOM解析的XML,我的代碼和輸出。我需要從「簡單數據」中獲取信息,但我沒有這麼做。瞭解用於Java的DOM解析器

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

您的節點「名稱」是「文件夾」節點的子節點,您如何將它視爲「文檔」節點的子節點。與其他節點的問題一樣,這就是爲什麼你在任何地方看到#text。 – Arham

+0

有2個名爲name的節點,一個是文檔的第一個孩子,另一個是地標的第一個孩子,但我不想發佈完整的xml,因爲它很長。實際上使這3種節點稱爲名稱。 1)文件2的第1個孩子)文件夾的第1個孩子以及3)地標的第1個孩子 –

+0

項目索引從0開始而不是從1開始 – shyam

回答

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");。請相應地修復你的遍歷。

+0

再次感謝Yogendra;),getTextContent()是我的主要問題。 –

+0

很高興知道。謝謝! –

0

我不能肯定的你到底想要什麼。也許再詳細一點。

org.w3c.Node有一個方法getTextContent()。一般來說,這些w3c類使用item(i)的劇組,例如Element

要跳過空白文本(節點名稱#文本),或只是更直接地訪問特定元素,就使用XPath。

+0

我需要得到$ 1.00和2小時的字符串出來的形式: $ 1.00 2hr