2013-08-12 1 views
0

에서 XML 속성 : -읽기 다중 노드 및 다음과 같이 내 XML은 자바

<myxml> 
    <resource name='book'> 
     <acl> 
      <ace person='bob' rights='rw' /> 
      <ace person='john' rights='w' /> 
     </acl> 
    </resource> 

    <resource name='dvd'> 
     <acl> 
      <ace person='bob' rights='w' /> 
     </acl> 
    </resource> 
</myxml> 

나는 문제가이 XML 문서를 읽는 데.

다음은 내가 시도한 코드입니다.

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory 
       .newInstance(); 
     DocumentBuilder documentBuilder = documentBuilderFactory 
       .newDocumentBuilder(); 
     Document document = documentBuilder.parse(new File(fileName)); 

    Element rootElement = xmlDocument.getDocumentElement(); 

    NodeList resourceList= rootElement.getElementsByTagName("resource"); 
    for (int i = 0; i < resourceList.getLength(); i++) { 
     Node node = resourceList.item(i); 
     Element element = (Element) node; 
     String resourceName= element.getAttribute("name"); 
    } 

기본적으로 "이 책은이 사용자가 xyz 권한으로 사용할 수 있습니다."와 같이 인쇄하고 싶습니다.

String objectName= element.getAttribute("name")으로 책의 이름을 알 수 있습니다. 이 후에 나는 갈 수 없어.

나는 자식 노드를 얻음으로써 시도했지만 null을 계속 얻는다.

제안 사항? 당신의 for 루프에서

NodeList aceList = element.getElementsByTagName("ace"); 
    for(int j= 0; j < aceList.getLength(); j++){ 
     Element ace = (Element) aceList.item(j); 
     String person = ace.getAttribute("person"); 
    } 
+0

나는 XPath를보고 제안했다. –

답변

0

하나의 옵션은 당신이 찾고있는 에이스 요소를 검색 할 수 element.getElementsByTagName ("에이스")를 사용하는 것입니다 for 루프 내부에 중첩되어 <ace> 요소를 처리합니다.

NodeList resourceList = rootElement.getElementsByTagName("resource"); 
for (int i = 0; i < resourceList.getLength(); i++) { 
    Element resource = (Element) resourceList.item(i); 
    String book = resource.getAttribute("name"); 
    NodeList aceList = resource.getElementsByTagName("ace"); 
    for (int j = 0; j < aceList.getLength(); j++) { 
     Element ace = (Element) aceList.item(j); 
     System.out.println("'" + book + "' can be used by '" 
       + ace.getAttribute("person") + "' with '" 
       + ace.getAttribute("rights") + "' permission."); 
    } 
} 

출력 :

'book' can be used by 'bob' with 'rw' permission. 
'book' can be used by 'john' with 'w' permission. 
'dvd' can be used by 'bob' with 'w' permission. 
0

:

for (int i = 0; i < resourceList.getLength(); i++) { 
    Element element = (Element)resourceList.item(i); 
    String resourceName= element.getAttribute("name"); 
    if (!resourceName.equals("book")) 
     continue; 
    NodeList acls = node.getElementsByTagName("acl"); 
    if (acls.getLength() == 0) 
     continue; 
    Element acl = (Element)acls.item(0); 
    if (acl == null) 
     continue; 
    NodeList aces = acl.getElementsByTagName("ace"); 
    for (int j = 0; j < aces.getLength(); j++) { 
     Element ace = (Element)aces.item(j); 
     // Process list 'aces' with people names 
     ... 
    } 
} 
0

당신은 또 다른 필요

+0

Worked : :) thanks. 감사합니다. – sagar

+0

친구를 도와주는 것이 기쁘다. 대답을 수락하십시오. 원한다면 [둘러보기] (http://stackoverflow.com/about)를 가져 가십시오. 에 오신 것을 환영합니다. –