Java Nodelist.item返回null

Java Nodelist.item返回null,java,xml,parsing,xml-parsing,Java,Xml,Parsing,Xml Parsing,下面是我读取xml文件.System.out.println(节点)的主要代码,在1.5和1.6中返回null。我发布了一个示例程序来重现这个错误。在代码之后,我也将我的xml放入了jdk 1.4中。具有相同代码和xml的程序在jdk 1.4中运行良好。但在更改为上述版本后,它返回null public class Main { private static Document document; public static void main(String[]

下面是我读取xml文件.System.out.println(节点)的主要代码,在1.5和1.6中返回null。我发布了一个示例程序来重现这个错误。在代码之后,我也将我的xml放入了jdk 1.4中。具有相同代码和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;
    }
}
公共类主
{        
私有静态文档;
公共静态void main(字符串[]args)抛出ParserConfiguration异常、SAXException、IOException
{
文件xmlfile=新文件(“D:\\utility_1341173385278.xml”);
parseXMLFile(xmlfile);
getNodes(getRootNode(),“DIR”);
}
公共静态void parseXMLFile(文件文件)抛出ParserConfiguration异常、SAXException、IOException
{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
document=db.parse(文件);
}
//获取xml中的根节点
公共静态节点getRootNode()
{
return document.getDocumentElement();
}
//要获取xml文件中从根节点开始的节点并打印以给定名称开始的所有节点。。。。。
公共静态列表getNodes(节点父节点、字符串nodeName)
{
List newList=newarraylist();
NodeList list=parent.getChildNodes();
如果(列表!=null)
{
对于(int i=0;i
xml文件包含目录、数据库和文件标记

    <?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上执行了这项操作。下面是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: 
        ]

****************************************
1.5年的产出
[#正文:
]
[目录:空]
[#正文:
]
[#正文:
]
[目录:空]
[#正文:
]
[#正文:
]

所以他们更改了toString()实现。你为什么还要依赖这个

创建您自己的节点包装以覆盖toString(),或者获取您感兴趣的属性。

而不是执行以下操作:

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


异常在哪里抛出?请发布堆栈跟踪。那里有一个输入错误:
list.item(i)
而不是
list.itme(i)
Element value = (Element) list.item(i);
System.out.println(value.getTextContent());