Java Jdom不会打印所有元素,而是打印空字符串

时间:2018-12-03 09:32:46

标签: java xml pom.xml element document

首先,我有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)为什么只打印一个“书”元素,而另一个是空的?

非常感谢。

3 个答案:

答案 0 :(得分:2)

这是因为嵌套了<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 :(得分:2)

  

(1)为什么打印的第一个元素是“ b11”而不是“ c01”?这是第一个元素。

对我来说不是。我得到了c01,然后有一个空白条目,对于输入而言,这很有意义。

  

(2)为什么只打印一个“书”元素,而另一个是空的?

因为您在其他book个元素中有book个元素。 getElementsByTagName返回所有四个。第二个是嵌套在第一个中的一个:

<?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的子节点,则会得到预期的输出:< / p>

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元素,而是列出了尽可能多的元素。

答案 2 :(得分:0)

您的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>