2014-08-27 2 views
0

xml 파일이 있고 제목과 첫 번째 작성자를 얻으려고합니다. java xml 하위 노드에서 속성 값을 가져 오는 중

는 데이터 메신저와 협력 :

<citation type="Book" date="1986" name="Book name"> 

     <title> 
      a fun name for a book 
     </title> 

     <authorList> 
      <person name="Person 1"/> 
      <person name="Person 2"/> 
      <person name="Person 3"/> 
     </authorList> 

    </citation> 

    <citation type="Book" date="1986" name="Another book"> 

     <title> 
      a boring book title 
     </title> 

     <authorList> 
      <person name="Person A"/> 
      <person name="Person B"/> 
      <person name="Person C"/> 
     </authorList> 

    </citation> 

내가

NodeList itemsCitation = doc.getElementsByTagName("citation"); 

for (int i = 0; i < itemsCitation.getLength(); i++) { 
Node n = itemsCitation.item(i); 

if (n.getNodeType() == Node.ELEMENT_NODE) { 

Element e = (Element) n; 
NodeList titleNode = e.getChildNodes(); 

for (int j = 0; j < titleNode.getLength(); j++) { 
     Node n2 = titleNode.item(j); 

     if (n2.getNodeType() == Node.ELEMENT_NODE && n2.getNodeName().equals("title")) { 
       System.out.println(n2.getTextContent()); 
     } 

     if (n2.getNodeType() == Node.ELEMENT_NODE && n2.getNodeName().equals("authorList")) { 

       // code i dont have 

     } 
    } 
} 

} 
} catch (Exception e) { 
e.printStackTrace(); 
을 쓴 코드

임려고 출력은 다음과 같습니다

a fun name for a book 
Person 1 
a boring book title 
Person A 

제목의 ISN '를 얻기 문제는 아니지만 첫 번째 저자를 얻는 것이 중요합니다. TextValent를 얻으려고하면 NodeValue를 얻으려고하면 "null"이됩니다. 빈 행만 얻습니다. 누군가가 나를 도울 수 있기를 바랍니다. 미리 감사드립니다!

+1

아, 내 JAXB를 사용하면 간단하지 않을까요? – laune

답변

0
Element root = document.getDocumentElement(); 
NodeList itemsCitation = root.getElementsByTagName("citation"); 
for(int it = 0; it < itemsCitation.getLength(); it++){ 
    Node cit = itemsCitation.item(it); 
    Element elCit = (Element)cit; 
    Node title = elCit.getElementsByTagName("title").item(0); 
    Element elTitle = (Element)title; 
    System.out.println("Title: " + elTitle.getTextContent()); 
    Node authorList = elCit.getElementsByTagName("authorList").item(0); 
    Element elAuthorList = (Element)authorList; 
    NodeList authors = elAuthorList.getElementsByTagName("person"); 
    for(int ia = 0; ia < authors.getLength(); ia++){ 
     Element elAuthor = (Element)authors.item(ia); 
     System.out.print(" " + elAuthor.getAttribute("name")); 
    } 
    System.out.println(); 
} 

필자는 필요한 노드 검사 및 기타 온 전성 검사를 생략했습니다.

JAXB를 살펴보십시오.

0

시도해보십시오. 원하는 경우 최적화 할 수 있습니다 :)

 NodeList nodeList = document.getElementsByTagName("citation"); 

     for (int i = 0; i < nodeList.getLength(); i++) { 

      Element citationChildNode = (Element)nodeList.item(i); 

      NodeList childNodeList = citationChildNode.getChildNodes(); 
      if(childNodeList == null){ 
       continue; 
      } 

      for(int j = 0; j < childNodeList.getLength(); j++) { 
       Node node = childNodeList.item(j); 

       if(node.getNodeName().equalsIgnoreCase("title")){ 
        Node n = node.getFirstChild(); 
        System.out.println(n.getNodeValue()); 
       }else if(node.getNodeName().equalsIgnoreCase("authorList")){ 

        NodeList authorList = node.getChildNodes(); 
        Node parent = authorList.item(0); 
        while(true) { 
         parent = parent.getNextSibling(); 
         if(parent == null){ 
          break; 
         } 
         NamedNodeMap map = parent.getAttributes(); 
         if(map == null){ 
          continue; 
         } 
         System.out.println(map.getNamedItem("name").getNodeValue()); 
         break; 
        } 
       } 
      } 
     } 
관련 문제