当前位置: 技术问答>java相关
有没有将一个XML文档转换为一棵JTree显示的例子,拜托拜托!
来源: 互联网 发布时间:2015-11-07
本文导语: 有没有将一个XML文档转换为一棵JTree显示的例子,拜托拜托! | 不好意思这是我以前做的程序不知道现在验证的部分好不好使了 希望对你有帮助 /* * TreeXmlView * User: yanglongfei * Date: Oct...
有没有将一个XML文档转换为一棵JTree显示的例子,拜托拜托!
|
不好意思这是我以前做的程序不知道现在验证的部分好不好使了
希望对你有帮助
/*
* TreeXmlView
* User: yanglongfei
* Date: Oct 29, 2001
* Time: 8:51:39 AM
* @author yanglongfei
* Tools:Idea
*/
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
import javax.swing.*;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TreeXmlView extends JFrame {
JTree tree;
Document dom = null;
public TreeXmlView() throws Exception {
super("TreeXmlView");
setSize(300, 300);
setLayout();
WindowAdapter closing = new WindowAdapter() {
public void windowClosing(WindowEvent ew) {
System.exit(0);
}
};
addWindowListener(closing);
setVisible(true);
}
public void setLayout() throws Exception {
dom = getDocument();
MyTreeModel mm = new MyTreeModel(dom);
tree = new JTree(mm);
tree.setCellRenderer(new MyDefaultCellRenderer());
JScrollPane jsp = new JScrollPane();
jsp.getViewport().add(tree);
getContentPane().add(jsp, BorderLayout.CENTER);
}
public Document getDocument() throws Exception {
// DocumentBuilderFactory doc = DocumentBuilderFactory.newInstance();
// DocumentBuilder dom = doc.newDocumentBuilder();
// Document parser = dom.parse("book.xml");
DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation ", true);
parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
parser.parse("book.xml");
Document doc = parser.getDocument();
return doc;
}
public static void main(String args[]) throws Exception {
TreeXmlView txv = new TreeXmlView();
}
}
class MyTreeModel implements TreeModel {
private Document root = null;
public MyTreeModel(Document root) {
this.root = root;
}
public Object getRoot() {
return root;
}
public Object getChild(Object parent, int index) {
return ((Node) parent).getChildNodes().item(index);
}
public int getChildCount(Object parent) {
return ((Node) parent).getChildNodes().getLength();
}
public boolean isLeaf(Object node) {
return ((Node) node).getChildNodes().getLength() == 0;
}
public void valueForPathChanged(TreePath path, Object newValue) {
}
public int getIndexOfChild(Object parent, Object child) {
NodeList nl = ((Node) parent).getChildNodes();
for (int i = 0; i
希望对你有帮助
/*
* TreeXmlView
* User: yanglongfei
* Date: Oct 29, 2001
* Time: 8:51:39 AM
* @author yanglongfei
* Tools:Idea
*/
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
import javax.swing.*;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TreeXmlView extends JFrame {
JTree tree;
Document dom = null;
public TreeXmlView() throws Exception {
super("TreeXmlView");
setSize(300, 300);
setLayout();
WindowAdapter closing = new WindowAdapter() {
public void windowClosing(WindowEvent ew) {
System.exit(0);
}
};
addWindowListener(closing);
setVisible(true);
}
public void setLayout() throws Exception {
dom = getDocument();
MyTreeModel mm = new MyTreeModel(dom);
tree = new JTree(mm);
tree.setCellRenderer(new MyDefaultCellRenderer());
JScrollPane jsp = new JScrollPane();
jsp.getViewport().add(tree);
getContentPane().add(jsp, BorderLayout.CENTER);
}
public Document getDocument() throws Exception {
// DocumentBuilderFactory doc = DocumentBuilderFactory.newInstance();
// DocumentBuilder dom = doc.newDocumentBuilder();
// Document parser = dom.parse("book.xml");
DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation ", true);
parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
parser.parse("book.xml");
Document doc = parser.getDocument();
return doc;
}
public static void main(String args[]) throws Exception {
TreeXmlView txv = new TreeXmlView();
}
}
class MyTreeModel implements TreeModel {
private Document root = null;
public MyTreeModel(Document root) {
this.root = root;
}
public Object getRoot() {
return root;
}
public Object getChild(Object parent, int index) {
return ((Node) parent).getChildNodes().item(index);
}
public int getChildCount(Object parent) {
return ((Node) parent).getChildNodes().getLength();
}
public boolean isLeaf(Object node) {
return ((Node) node).getChildNodes().getLength() == 0;
}
public void valueForPathChanged(TreePath path, Object newValue) {
}
public int getIndexOfChild(Object parent, Object child) {
NodeList nl = ((Node) parent).getChildNodes();
for (int i = 0; i