2012-10-20 5 views
0

다음은 DOM, 내 코드 및 출력과 구문 분석하려는 XML입니다. "간단한 데이터"에서 정보를 가져와야하지만 그렇게하지 못합니다.DOM 파서 for Java의 이해

XML : 디버깅 목적으로 sysouts 가득

<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> 

코드 :

 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

name이라는 노드가 두 개 있습니다. 하나는 문서의 첫 번째 자식이고, 다른 하나는 장소 표시의 첫 번째 자식입니다.하지만 XML의 길이가 길어서 전체 XML을 게시하고 싶지 않았습니다. 실제로 name이라는 3 가지 유형의 노드를 만듭니다. 1) 문서의 첫 번째 자녀 2) 폴더의 첫 번째 자녀 및 3) 아이콘 –

+0

의 첫 번째 자녀 항목 색인은 1에서 시작하지 않고 0에서 시작합니다. – 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이 사용됩니다. 그에 따라 트래버 설을 수정하십시오.

+0

다시 한번 감사 요겐 2 시간), getTextContent()가 된 내 주요 문제. –

+0

잘 알고 있습니다. 감사! –

0

난 당신이 정확하게 원하는 특정 아닙니다. 어쩌면 좀 더 정교해질 수도 있습니다.

org.w3c.Node의 메서드는 getTextContent()입니다. 일반적으로이 w3c 클래스는 예를 들어 Element과 같이 item(i)의 캐스트를 사용합니다.

공백 문자 (노드 이름 # 텍스트)를 건너 뛰거나 단순히 특정 요소에 직접 액세스하려면 XPath를 사용합니다.

+0

나는 밖으로 문자열의 형태로 $ 1.00 2 시간을 얻을 필요가 : $ 1.00