2016-07-04 96 views
0

我有方法:如何獲取屬性值?的DOMParser

private static void print(NodeList nodeList) { 

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

     if (t.getNodeType() == Node.ELEMENT_NODE) { 
      System.out.println("node: " + t.getNodeName()); 
      System.out.println("values " + t.getTextContent()); 
      System.out.println("------------------------------"); 
     } 



     if (doc.hasChildNodes()) { 
      print(t.getChildNodes()); 
     } 
    } 

} 

它顯示XML文檔的內容:

<Card> 
     <Thema>people</Thema> 
     <Type sent="true">advertising</Type> 
     <Country>India</Country> 
     <Year>1966</Year> 
     <Authors><Author>Julia</Author></Authors> 
     <Valuable>historical</Valuable> 
    </Card> 

但不顯示在「已發送」的節點屬性的值。我如何修改它? 謝謝!

回答

0

使用方法getAttributes得到一個節點的屬性列表:包含此節點的屬性

NamedNodeMap(如果它是Element)或否則返回null。

+0

當我嘗試這樣做時,我得到「空指針異常」 –

+0

檢查您是否將此方法應用於空節點。或者如果您嘗試循環到此調用的空結果 –