2012-09-15 2 views
0

내가 이런 하나의 XML ... XML의 자식 노드를 계산하는 방법은 무엇입니까?

<?xml version="1.0" encoding="iso-8859-1"?> 
<world> 
    <country> 
     <name> France </name> 
     <city> Paris </city> 
     <population> 3996 </population> 
     <city> Lille </city> 
     <state>NE</state> 
     <zip> 000000 </zip> 
    </country> 
</world> 

여기 우리가 태그의 국가 (6) 아이가 볼 수있는 것은 프로그래밍을 계산하는 방법을 나누었다 노드. 당신의 도움이 세트의 크기를 얻으려면 ... 미리 감사합니다 ....

+0

제 대답을 참조하면 문제가 해결됩니다. –

답변

0

사용하는 방법에 대해 살펴 NodeList의 하위 노드 수를 계산하면 문제가 해결됩니다.

URL url = new URL("http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml"); 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(new InputSource(url.openStream())); 
doc.getDocumentElement().normalize(); 
NodeList nodeList = doc.getElementsByTagName("country"); 
int i=nodeList.getLength(); 
System.out.println("Total Child is:- " + i); 

그리고 Dom Parser를 사용한 XML 구문 분석에 대한 자세한 내용은 아래 링크를 참조하십시오.

XML Parsing Using DOM Parser

0

을 이해할 수있을 것이다 :

nl.getLength() //n1 is the object of NodeList 

public int getLength() 

The number of nodes in the list. The range of valid child node indices is 0 to length-1 inclusive. 

을 또한 코드 아래 NODELIST

관련 문제