如何构建DOM树?

时间:2013-04-10 16:18:47

标签: dom xml-parsing tree

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE people SYSTEM "validator.dtd">

<people>
    <student>
        <name>John</name>
        <course>Computer Technology</course>
        <semester>6</semester>
        <scheme>E</scheme>
    </student>

    <student>
        <name>Foo</name>
        <course>Industrial Electronics</course>
        <semester>6</semester>
        <scheme>E</scheme>
    </student>
</people>    

这是我的XML文档。我已经用树表示了这一点 enter image description here

此树表示是否正确或是否错误?

1 个答案:

答案 0 :(得分:3)

只需打印一个DOM树就可以获得一个好主意:

public static void main(String[] args) throws UnsupportedEncodingException, IOException, ParserConfigurationException, SAXException {
    final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
            + "<people>"
            + "    <!-- a comment -->"
            + "    <student>"
            + "        <name>John</name>"
            + "        <!-- a comment -->"
            + "        <course>Computer Technology</course>"
            + "        <semester>6</semester>"
            + "        <scheme>E</scheme>"
            + "    </student>"
            + ""
            + "    <student>"
            + "        <name>Foo</name>"
            + "        <course>Industrial Electronics</course>"
            + "        <semester>6</semester>"
            + "        <scheme>E</scheme>"
            + "    </student>"
            + "</people>";
    final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
    printNodes(document.getDocumentElement(), 0);
}

private static void printNodes(final Node node, final int depth) {
    final StringBuilder prefix = new StringBuilder();
    for (int i = 0; i < depth; ++i) {
        prefix.append("\t");
    }
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        System.out.println(prefix.toString() + "Going into " + node.getNodeName());
        final NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); ++i) {
            printNodes(nodeList.item(i), depth + 1);
        }
    } else if (node.getNodeType() == Node.COMMENT_NODE) {
        System.out.println(prefix.toString() + "Comment node: \"" + node.getTextContent() + "\"");
    } else {
        System.out.println(prefix.toString() + "Text node: \"" + node.getTextContent() + "\"");
    }
}

这个输出是:

Going into people
    Text node: "    "
    Comment node: " a comment "
    Text node: "    "
    Going into student
        Text node: "        "
        Going into name
            Text node: "John"
        Text node: "        "
        Comment node: " a comment "
        Text node: "        "
        Going into course
            Text node: "Computer Technology"
        Text node: "        "
        Going into semester
            Text node: "6"
        Text node: "        "
        Going into scheme
            Text node: "E"
        Text node: "    "
    Text node: "    "
    Going into student
        Text node: "        "
        Going into name
            Text node: "Foo"
        Text node: "        "
        Going into course
            Text node: "Industrial Electronics"
        Text node: "        "
        Going into semester
            Text node: "6"
        Text node: "        "
        Going into scheme
            Text node: "E"
        Text node: "    "

正如您所看到的,在可见节点之间到处都有文本节点。这是因为理论上你可以在周围有个子节点 - 例如

<student>
    some random text
    <course>Computer</course>
    some more text
</student>

因此DOM树需要考虑到这一点。如果XML不是很漂亮但只是一行,那么下一个节点将是空的而不是空白。

弄清楚文档,看看它对输出有什么影响。