dom 树的后序遍历
如果有一个名为a.xml的XML文件,有没有办法遍历它的DOM树 后购时尚?
我尝试使用 GetNextSiblings 方法,但它不起作用。有什么想法吗?
这是 XML:
<?xml version="1.0" encoding="UTF-8"?>
<title text="title1">
<comment id="comment1">
<data> abcd </data>
<data> efgh </data>
</comment>
<comment id="comment2">
<data> ijkl </data>
<data> mnop </data>
<data> qrst </data>
</comment>
</title>
这是我的遍历它的代码:
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.SAXException;
public class Newtraverse {
public static Node check(Node node){
Node c=node;
// Node c = null;
if (node!=null)
if (node.hasChildNodes()==true &&node.getNodeName()!=null)
{
node=node.getFirstChild().getNextSibling();
if (node!=null)
{
System.out.println(node);
check(node);
}
if(node==null)
{
c=c.getNextSibling();
check(c);
}
}
return node;
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
File file = new File("d:\\a.xml");
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
document.getDocumentElement().normalize();
Node b=document.getFirstChild();
Node result= check(b);
}
}
这是输出:
[comment: null]
[data: null]
正如大家所看到的,它只遍历了两个标签。我该如何解决这个问题?
If there is an XML file called a.xml, is there any way to traverse its DOM tree in
postorder fashion?
I tried using GetNextSiblings method but it didn't work. Any idea?
Here is the XML:
<?xml version="1.0" encoding="UTF-8"?>
<title text="title1">
<comment id="comment1">
<data> abcd </data>
<data> efgh </data>
</comment>
<comment id="comment2">
<data> ijkl </data>
<data> mnop </data>
<data> qrst </data>
</comment>
</title>
And here is my code to traverse it:
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.SAXException;
public class Newtraverse {
public static Node check(Node node){
Node c=node;
// Node c = null;
if (node!=null)
if (node.hasChildNodes()==true &&node.getNodeName()!=null)
{
node=node.getFirstChild().getNextSibling();
if (node!=null)
{
System.out.println(node);
check(node);
}
if(node==null)
{
c=c.getNextSibling();
check(c);
}
}
return node;
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
File file = new File("d:\\a.xml");
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
document.getDocumentElement().normalize();
Node b=document.getFirstChild();
Node result= check(b);
}
}
And here is the output:
[comment: null]
[data: null]
As you all can see, it just traverses two tags. How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是你的检查方法应该是什么样的(虽然我没有运行它......)
Here's how your check method should like (I didn't run it though...)
您的
check
函数执行以下操作:因此,您首先检查
title
,title
的第二个子节点是comment
具有三个data
子元素的元素。comment
节点。data
元素。data
节点。null
)您可以通过不每次都跳过第一个子节点来解决此问题。您可能还希望循环子节点,每次递归下一个级别,而不是对所有内容都使用递归。
Your
check
function does this:So, You start by checking
title
,title
's second child is thecomment
element with threedata
child elements.comment
node.data
element with content "mnop".data
node.null
)You can fix this by not skipping the first child node every time. You may also wish to loop over the child nodes, recursing the next level each time, rather than using recursion for everything.