【发布时间】:2019-05-04 13:50:04
【问题描述】:
首先,我有 xml 文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bookshelf>
<book ISBN="c01" press="AD press">
<book>Oracle</book>
<Author>Smith</Author>
<price>32.00</price>
</book>
<book ISBN="b11" press="XY press">
<book>Android</book>
<Author>Smith</Author>
<price>35.00</price>
</book>
</bookshelf>
然后有java代码:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(currentPath + "/book.xml");
for (int i = 0; i < 2; ++i) {
System.out.println("begin");
Node n = document.getElementsByTagName("book").item(i);
Element e = (Element) n;
System.out.println(e.getAttribute("ISBN"));
System.out.println(e.getAttribute("press"));
System.out.println("end");
}
然后打印:
begin
b11
XY press
end
begin
end
这对我来说很奇怪:
(1) 为什么打印的第一个元素是“b11”而不是“c01”?这是第一个元素。
(2)为什么只打印一个“书”元素,另一个是空的?
非常感谢。
【问题讨论】:
标签: java xml pom.xml element document