2013-03-26 3 views
2

의 이름 속성을 가져올 수 없습니다 :나는이처럼 보이는 XML 파일이 childNode에

<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist"> 
<exist:collection name="/db/RCM" created="2013-03-24T09:37:34.957+05:30" owner="admin" group="dba" permissions="rwxrwxrwx"> 
<exist:resource name="demo2.xml" created="2013-03-24T09:44:13.696+05:30" last-modified="2013-03-24T09:44:13.696+05:30" owner="guest" group="guest" permissions="rw-r--r--"/> 
<exist:resource name="demo3.xml" created="2013-03-24T09:45:47.592+05:30" last-modified="2013-03-24T09:45:47.592+05:30" owner="guest" group="guest" permissions="rw-r--r--"/> 
<exist:resource name="rcmdemo.xml" created="2013-03-25T11:36:45.659+05:30" last-modified="2013-03-25T11:36:45.659+05:30" owner="guest" group="guest" permissions="rw-r--r--"/> 
<exist:resource name="rcmdemo2.xml" created="2013-03-25T11:47:03.564+05:30" last-modified="2013-03-25T11:47:03.564+05:30" owner="guest" group="guest" permissions="rw-r--r--"/> 
</exist:collection> 
</exist:result> 

내가 XML 파일의 이름을 가져 오려는를, 그래서 출력은 다음과 같습니다

demo2.xml 
demo3.xml 
rcmdemo.xml 
rcmdemo2.xml 

나는 다음과 같은 코드를 작성했습니다 :

NodeList nodeList = doc.getElementsByTagName("exist:resource"); 
for (int i = 0; i < nodeList.getLength(); i++) { 
    Node n = nodeList.item(i); 
    Node actualNode = n.getFirstChild(); 
    if (actualNode != null) { 
     System.out.println(actualNode.getNodeValue()); 
    } 
} 

을하지만 어디 잘못 갈거야, 내가 원하는 출력을 반환하지 않습니다?

+0

저는 name이 childnode라고 생각하지 않습니다. 주어진 노드의 속성을 확인하십시오. – smk

답변

1

당신은 nameexist:resource의 속성 당신 때문에 주어진 노드의 속성을 얻을 수 있습니다.

NodeList nodeList = doc.getElementsByTagName("exist:resource"); 
     for (int i = 0; i < nodeList.getLength(); i++) { 
      Node n = nodeList.item(i); 
      Node actualNode = n.getFirstChild(); 
      if (actualNode != null) { 
       // Will return node value 
       System.out.println(actualNode.getNodeValue()); 
       // Will return the attribute value 
       System.out.println(current.getAttributeValue("name")); 
      } 
     } 
2

이 예제에서 이름은 노드의 이름이 아니라 노드의 속성입니다. 노드의 속성에 관한 정보는 다음 질문을 살펴보십시오. 특히 두 번째 대답은 내가 생각하는 것입니다.

get the the attributes from an XML File using Java

관련 문제