您可以只打印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不是非常漂亮,而只是一行,那麼下一個節點將是空的而不是空白。
翻閱文檔,看看它對輸出有什麼影響。
那麼,學生「Foo」不在你的圖表中,所以它看起來不完全正確。 – Henrik 2013-04-10 16:21:18
@Henrik我知道,我忽略了空間限制 – 2013-04-10 16:21:49
你忘記了每個子節點前後的空文本節點。爲什麼不構建DOM然後打印出來? – 2013-04-10 16:23:12