2012-07-25 410 views
0

BElow是我读取xml文件的主要代码.System.out.println(node)在1.5和1.6中返回null。我发布了一个示例程序来重现该bug。我的xml也是如此。具有相同代码和xml的程序在jdk 1.4中工作正常。但在更改为上述版本后,它将返回null。Nodelist.item返回null

public class Main 
{   
    private static Document document; 

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException 
    { 
     File xmlfile = new File("D:\\utility_1341173385278.xml"); 

     parseXMLFile(xmlfile); 
     getNodes(getRootNode(), "DIR"); 
    } 

    public static void parseXMLFile(File file) throws ParserConfigurationException, SAXException, IOException 
    { 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     document = db.parse(file); 
    } 

    // get root node in xml 

    public static Node getRootNode() 
    { 
     return document.getDocumentElement(); 
    } 

    // To get nodes in xml file starting from root node and prints all nodes which starts with given name..... 

    public static List getNodes(Node parent, String nodeName) 
    { 
     List newList = new ArrayList(); 

     NodeList list = parent.getChildNodes(); 
     if (list != null) 
     { 
      for (int i = 0; i < list.getLength(); i++) 
      { 
       Node node = list.item(i); 
       System.out.println(node); 
       if (node.getNodeName().equals(nodeName)) 
       { 
        newList.add(node); 
       } 
      } 
     } 

     list = null; 

     return newList; 
    } 
} 

XML文件与DIR,数据库和文件标签

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
    <DATABASE data="" name="Test" path="file:/D:/Java/bu%2077/Test/utility/"> 
     <DIR last_mod_date="1341058205411" name="utility" status="unchanged"> 
      <DIR last_mod_date="1341058205445" name="bs90" status="unchanged"> 
       <DIR last_mod_date="1341058205699" name=".r280" status="unchanged"> 
        <DIR last_mod_date="1341058205756" name="0093" status="unchanged"> 
         <FILE last_mod_date="1340873680000" name="c2c1f5f1.000004b6" size="3360" status="unchanged"/> 
         <FILE last_mod_date="1340873680000" name="c7c1f0f1.00000cba" size="3440" status="unchanged"/> 
        </DIR> 
       </DIR> 
       <FILE last_mod_date="1340957268000" name="New Text Document.txt" size="5" status="unchanged"/> 
      </DIR> 
     </DIR> 
    </DATABASE> 

我已经从1.4上执行这一点,1.5.Below是1.4

  <DIR last_mod_date="1341058205411" name="utility" status="unchanged"> 
       <DIR last_mod_date="1341058205445" name="bs90" status="unchanged"> 
        <DIR last_mod_date="1341058205699" name=".r280" status="unchanged"> 
         <DIR last_mod_date="1341058205756" name="0093" status="unchanged"> 
          <FILE last_mod_date="1340873680000" name="c2c1f5f1.000004b6" size="3360" status="unchanged"/> 
          <FILE last_mod_date="1340873680000" name="c7c1f0f1.00000cba" size="3440" status="unchanged"/> 
         </DIR> 
        </DIR> 
        <FILE last_mod_date="1340957268000" name="New Text Document.txt" size="5" status="unchanged"/> 
       </DIR> 
      </DIR> 


**************************************** 
output in 1.5 

[#text: 
    ] 
[DIR: null] 
[#text: 
] 
[#text: 
    ] 
[DIR: null] 
[#text: 
] 
[#text: 
     ] 
+0

在哪里异常抛出?请发布堆栈跟踪。 – aioobe 2012-07-25 06:06:27

回答

0

输出,使他们”已经改变了toString()实现。你为什么依靠那个呢?

无论而是创建自己的节点包装覆盖的toString()或让你感兴趣的物业

2

做的:

Node node = list.item(i); 
System.out.println(node); 

尝试:

Element value = (Element) list.item(i); 
System.out.println(value.getTextContent()); 
+0

这里有一个错字:'list.item(i)'而不是'list.itme(i)' – verybadalloc 2013-06-20 22:10:58