使用DOM解析器从XML文档创建JTree [英] Creating a JTree out of an XML document using DOM parser
问题描述
package xml;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.io.*;
public class ThirdParser extends JFrame{
DocumentBuilderFactory factory;
DocumentBuilder builder;
File f;
Document d;
JTree tree;
JScrollPane scroll;
//------------------------------------------------------------------------------
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override public void run(){
new ThirdParser();
}
});
}
//------------------------------------------------------------------------------
public ThirdParser(){
try{
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
f = new File("E:/Website Projects/XML/helloWorld.xml");
d = builder.parse(f);
String people = "people";
DefaultMutableTreeNode node = new DefaultMutableTreeNode(people);
tree = new JTree(node);
Element e = d.getDocumentElement();
if(e.hasChildNodes()){
DefaultMutableTreeNode root = new DefaultMutableTreeNode
(e.getTagName());
NodeList children = e.getChildNodes();
for(int i=0;i<children.getLength();i++){
Node child = children.item(i);
visit(child,root);
}
}
}catch(ParserConfigurationException e){
e.printStackTrace();
}catch(SAXException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
// scroll = new JScrollPane(tree);
this.add(tree);
this.setVisible(true);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//------------------------------------------------------------------------------
public void visit(Node child,DefaultMutableTreeNode parent){
short type = child.getNodeType();
if(type == Node.ELEMENT_NODE){
Element e = (Element)child;
DefaultMutableTreeNode node = new DefaultMutableTreeNode
(e.getTagName());
parent.add(node);
if(e.hasChildNodes()){
NodeList list = e.getChildNodes();
for(int i=0;i<list.getLength();i++){
visit(list.item(i),node);
}
}
}else if(type == Node.TEXT_NODE){
Text t = (Text)child;
String textContent = t.getTextContent();
DefaultMutableTreeNode node = new DefaultMutableTreeNode(
textContent);
parent.add(node);
}
}
//------------------------------------------------------------------------------
}
这是我的代码来解析一个XML文档,并将其表示为一个 JTree
。问题是我只得到 JTree
中的根节点,没有别的。我试图用类似于这个代码的代码去执行目录结构,这样做有效。我不知道为什么这不给我我期望的结果。
This is my code to parse an XML document and represent it as a JTree
. The problem is that I only get the root node in the JTree
and nothing else. I tried walking the directory structure with a code similar to this and that worked. I do not know why this does not give me the result I expect.
<?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>
注意:如果输入 System.out.println()
在 visit()
方法中打印元素和文本节点,它打印正常。只是节点不被添加。
Note: If I enter System.out.println()
in the visit()
method to print element and text nodes, it prints fine. Just that nodes are not added.
推荐答案
看起来你正在将孩子添加到错误的父节点。树根设置为 tree = new JTree(node);
,但是您可以将子添加到 DefaultMutableTreeNode root = new DefaultMutableTreeNode(e.getTagName() );
根本不是树的一部分。快速修复将会改变:
Looks like you are adding the children to the wrong parent node. The tree root is set tree = new JTree(node);
, but then you add children to DefaultMutableTreeNode root = new DefaultMutableTreeNode(e.getTagName());
which is not part of the tree at all. Quick fix would be changing:
visit(child,root);
至
visit(child,node);
或执行 node.add(root);
一旦所有节点被访问。
Or execute node.add(root);
once all nodes were visited.
这篇关于使用DOM解析器从XML文档创建JTree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!