【发布时间】:2018-12-04 03:13:42
【问题描述】:
我有一个如下的xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bookshelf>
<book ISBN="a01" press="AD press"/>
<book ISBN="a11" press="BC press">
<book>JavaSE</book>
<Author>John</Author>
<price>35.00</price>
</book>
<book ISBN="b11" press="XY press">
<book>Android</book>
<Author>Smith</Author>
<price>35.00</price>
</book>
</bookshelf>
还有一个简单的Java dom程序如下:
System.out.println(currentPath);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(currentPath + "/book.xml");
Node node = document.getElementsByTagName("").item(1);
System.out.println(node);
(1) 打印出“null”,为什么?
(2) 我google了很多示例代码来调用document.getElementsByTagName(""),我想知道“”是代表根元素还是其他?
非常感谢。
【问题讨论】:
-
试试
document.getDocumentElement()
标签: java xml null document getelementsbytagname