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:
]
在哪裏異常拋出?請發佈堆棧跟蹤。 – aioobe 2012-07-25 06:06:27