Java jdom 不打印所有元素,打印空字符串
Java jdom doesn't print all elements, prints empty string
首先,我有 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)为什么只打印了一个"book"元素,另一个为空?
非常感谢。
这是因为嵌套了 <book>
标签。由于 <book>
标记位于 <book>
内,解析器将 <book>Oracle</book>
视为第二条记录。
<book ISBN="c01" press="AD press">
<book>Oracle</book> //<book> tag inside <book>
<Author>Smith</Author>
<price>32.00</price>
</book>
(1) Why the first element printed is "b11" but not "c01"? It's the first element.
对我来说没有。我得到了 c01
然后是一个空白条目,这对输入有意义。
(2) Why only one "book" element is printed, the other is empty?
因为在其他 book
个元素中有 book
个元素。 getElementsByTagName
returns 他们四个。第二个是嵌套在第一个中的那个:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bookshelf>
<book ISBN="c01" press="AD press"> #1 (index 0)
<book>Oracle</book> #2 (index 1)
<Author>Smith</Author>
<price>32.00</price>
</book>
<book ISBN="b11" press="XY press"> #3 (index 2)
<book>Android</book> #4 (index 3)
<Author>Smith</Author>
<price>35.00</price>
</book>
</bookshelf>
我不太熟悉这个特定的 API,但是如果我得到 bookshelf
然后循环它的子项并挑选出 book
的那些,我获得预期的输出:
import javax.xml.parsers.*;
import org.w3c.dom.*;
class Example {
public static final void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("book.xml");
NodeList bookshelves = document.getElementsByTagName("bookshelf");
if (bookshelves.getLength() > 0) {
Element bookshelf = (Element)bookshelves.item(0);
NodeList children = bookshelf.getChildNodes();
for (int i = 0, l = children.getLength(); i < l; ++i) {
Node child = children.item(i);
if (child.getNodeName().equals("book")) {
Element book = (Element)child;
System.out.println(book.getAttribute("ISBN"));
System.out.println(book.getAttribute("press"));
}
}
}
}
}
该代码采用单个书架,显然可以根据需要进行调整。它不假设只有两个 bookshelf > book
元素,它列出了尽可能多的元素。
你的 xml 有点奇怪,因为 <book>
代表两种不同的东西。
我的建议是用 <title>
作为标题而不是 <book>
。
来标记您的文档更具可读性
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bookshelf>
<book ISBN="c01" press="AD press">
<title>Oracle</title>
<Author>Smith</Author>
<price>32.00</price>
</book>
<book ISBN="b11" press="XY press">
<title>Android</title>
<Author>Smith</Author>
<price>35.00</price>
</book>
</bookshelf>
首先,我有 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)为什么只打印了一个"book"元素,另一个为空?
非常感谢。
这是因为嵌套了 <book>
标签。由于 <book>
标记位于 <book>
内,解析器将 <book>Oracle</book>
视为第二条记录。
<book ISBN="c01" press="AD press">
<book>Oracle</book> //<book> tag inside <book>
<Author>Smith</Author>
<price>32.00</price>
</book>
(1) Why the first element printed is "b11" but not "c01"? It's the first element.
对我来说没有。我得到了 c01
然后是一个空白条目,这对输入有意义。
(2) Why only one "book" element is printed, the other is empty?
因为在其他 book
个元素中有 book
个元素。 getElementsByTagName
returns 他们四个。第二个是嵌套在第一个中的那个:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bookshelf>
<book ISBN="c01" press="AD press"> #1 (index 0)
<book>Oracle</book> #2 (index 1)
<Author>Smith</Author>
<price>32.00</price>
</book>
<book ISBN="b11" press="XY press"> #3 (index 2)
<book>Android</book> #4 (index 3)
<Author>Smith</Author>
<price>35.00</price>
</book>
</bookshelf>
我不太熟悉这个特定的 API,但是如果我得到 bookshelf
然后循环它的子项并挑选出 book
的那些,我获得预期的输出:
import javax.xml.parsers.*;
import org.w3c.dom.*;
class Example {
public static final void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("book.xml");
NodeList bookshelves = document.getElementsByTagName("bookshelf");
if (bookshelves.getLength() > 0) {
Element bookshelf = (Element)bookshelves.item(0);
NodeList children = bookshelf.getChildNodes();
for (int i = 0, l = children.getLength(); i < l; ++i) {
Node child = children.item(i);
if (child.getNodeName().equals("book")) {
Element book = (Element)child;
System.out.println(book.getAttribute("ISBN"));
System.out.println(book.getAttribute("press"));
}
}
}
}
}
该代码采用单个书架,显然可以根据需要进行调整。它不假设只有两个 bookshelf > book
元素,它列出了尽可能多的元素。
你的 xml 有点奇怪,因为 <book>
代表两种不同的东西。
我的建议是用 <title>
作为标题而不是 <book>
。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bookshelf>
<book ISBN="c01" press="AD press">
<title>Oracle</title>
<Author>Smith</Author>
<price>32.00</price>
</book>
<book ISBN="b11" press="XY press">
<title>Android</title>
<Author>Smith</Author>
<price>35.00</price>
</book>
</bookshelf>