2011-04-27 6 views
0
<?xml version="1.0" encoding="UTF-8"?> 
<locations> 
    <location id="83"> 
     <location_name>name</location_name> 
     <company_name>company a</company_name> 
     <machines> 
      <machine id="12">A</machine> 
      <machine id="312">B</machine> 
     </machines> 
    </location> 
    <location id="85"> 
     <location_name>name</location_name> 
     <company_name>company c</company_name> 
     <machines> 
      <machine id="21">A</machine> 
      <machine id="45">B</machine> 
     </machines> 
    </location> 
</locations> 

나는 XOM을 사용하고 있으며 모든 위치 이름을 얻으려고하고 있지만 어떻게해야할지 모르겠다.XOM을 사용하는 어린이의 수당 받기

Element root = doc.getRootElement();  
int i = 1; 
Element child = (Element) root.getChild(i); 
while(child != null) 
{ 
    System.out.println(child.getFirstChildElement("location_name").getValue()); 
    child = (Element) root.getChild(i++); 
} 

하지만이 기능은 작동하지 않으며 첫 번째 이름 만 표시하고 두 번째 루프에서는 오류를 표시합니다.

그리고 두 번째 질문 : 무엇 getChildCount() 카운트 ??

+0

실마리를주십시오, 무엇이 오류입니까? –

+0

스택 트레이스를 보여 주시고, 진단하기가 아주 힘듭니다. –

답변

0

getChild(int i)은 i 번째 자식 요소 일 필요는없고 노드을 반환합니다. getChildElements("location")을 사용하고 반환 된 목록을 반복합니다. 또 다른 방법은 루트 요소에 대해 XPath 쿼리를 사용하는 것입니다.

또한 javadoc은 매우 유용한 정보입니다 : http://www.xom.nu/apidocs/.