gpt4 book ai didi

java - Nodelist.item 返回 null

转载 作者:行者123 更新时间:2023-12-02 07:40:44 27 4
gpt4 key购买 nike

下面是我读取 xml 文件的主要代码。System.out.println(node) 在 1.5 和 1.6 中返回 null。我正在发布一个示例程序来重现该错误。在代码之后,我也放置了我的 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 上都执行了此操作。下面是 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:
]

最佳答案

而不是这样做:

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

尝试:

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

关于java - Nodelist.item 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11643615/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com