2014-08-27 45 views
0

我得到了一個xml文件,並試圖獲得標題和第一作者。java xml從子節點獲取屬性值

這是一起工作的數據IM:

<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」這個問題,但得到第一個作者是。如果我嘗試獲取NodeValue,則只會得到「null」,如果我嘗試獲取TextConent,則只會獲得空行。我真的希望有人能幫助我。提前致謝!

+1

哦,我的,這不是簡單的使用JAXB ... – laune 2014-08-27 14:03:04

回答

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; 
        } 
       } 
      } 
     }