当前位置: 技术问答>java相关
哪为大哥帮我XML文件显示到一个是树上
来源: 互联网 发布时间:2015-11-10
本文导语: 哪为大哥帮我XML文件显示到一个是树上 | 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.swin...
哪为大哥帮我XML文件显示到一个是树上
|
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
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