Nodelist.item返回null

时间:2012-07-25 06:04:23

标签: java xml parsing xml-parsing

BElow是我读取xml文件的主要代码.System.out.println(node)在1.5和1.6中返回null。我发布了一个示例程序来重现bug。在代码之后我也放了我的xml。具有相同代码和xml的程序在jdk 1.4中工作正常。但是在更改为上面的版本之后它返回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,DATABASE和FILE标记

    <?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: 
        ]

2 个答案:

答案 0 :(得分:2)

而不是:

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

尝试:

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

答案 1 :(得分:0)

所以他们改变了toString()实现。你为什么还要依赖它?

创建自己的Node包装器以覆盖toString()或获取您感兴趣的属性。