⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmltree.java

📁 用dom解析xml文件,读到一颗Jtree 的树中,可供初学者参考
💻 JAVA
字号:
package swing.tree;

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.filechooser.FileFilter;

public class XmlTree {
	private JFrame frame;
	private Container contentPane;
	private JScrollPane Pane;
	private JMenuBar menuBar;
	private JMenu File;
	private JMenuItem open,exit;
	private String string,string2="",string1="";
	private DefaultMutableTreeNode node,node1;
	private NodeList nodes;
	private DefaultMutableTreeNode node22,node221;
	private JTree tree;
	
	public XmlTree() {
		frame = new JFrame("XML浏览器");
		frame.setBounds(0, 0, 1024, 738);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		contentPane = frame.getContentPane();
		
		initGUI();
	}
	
	public void initGUI() {
		frame.setLayout(null);
		contentPane.setLayout(null);
		menuBar=new JMenuBar();
		menuBar.setBounds(0, 0, 1024, 30);
		frame.add(menuBar);
		File=new JMenu("文件");
		menuBar.add(File);
		open=new JMenuItem("打开",new ImageIcon("open.gif"));
		File.add(open);
		exit=new JMenuItem("退出");
		File.addSeparator();
		File.add(exit);
		
		open.addActionListener(new FileOpenListener());
		
		exit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent m){
				System.exit(0);
			}
		});
	}
	
	class FileOpenListener implements ActionListener {
		public void actionPerformed(ActionEvent e){
		JFileChooser chooser=new JFileChooser();
		chooser.setCurrentDirectory(new File("."));
		chooser.setMultiSelectionEnabled(true);
		chooser.setAcceptAllFileFilterUsed(false);
		chooser.addChoosableFileFilter(new projectFilter());
		chooser.addChoosableFileFilter(new xmlFilter());
		int result=chooser.showOpenDialog(frame);
		string=chooser.getSelectedFile().getPath();
		try{
		tree(string);
		}catch(Exception n){
			
		}
		}
	}
	
	class xmlFilter extends FileFilter{
		public boolean accept(File f){
			return f.getName().toLowerCase().endsWith(".xml")|f.isDirectory();
		}
		public String getDescription(){
			return "XML file";
		}
	}
	
	class projectFilter extends FileFilter{
		public boolean accept(File f){
			return f.getName().toLowerCase().endsWith(".project")|f.isDirectory();
		}
		public String getDescription(){
			return "PROJECT file";
		}
	}
	
	public void tree(String string)throws Exception{
		FileInputStream fis=new FileInputStream(string);
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=factory.newDocumentBuilder();
		Document doc=builder.parse(fis);
		Element root=doc.getDocumentElement();
		if(root!=null){
			
			NamedNodeMap attributes=root.getAttributes();
			for(int i=0;i<attributes.getLength();i++){
				string2=string2+"   "+attributes.item(i).toString().trim();
				node1=new DefaultMutableTreeNode(string2);
				
			}
			if(node1!=null){
			node=new DefaultMutableTreeNode("<"+" "+root.getNodeName()+"  "+node1+" "+">");
			string2="";
			}
			else{
				node=new DefaultMutableTreeNode("<"+" "+root.getNodeName()+" "+">");
			}
			
			nodes=root.getChildNodes();
			child(nodes,node);
			
			tree=new JTree(node);
			tree.setShowsRootHandles(true);
			tree.setEditable(true);
			tree.putClientProperty("JTree.lineStyle", "None");
			tree.setRowHeight(20);
			if(Pane!=null){
				contentPane.remove(Pane);
				}
			Pane=new JScrollPane(tree);
			Pane.setBounds(0,25,1024,678);
			contentPane.add(Pane);
			frame.show();
			
		}
	}
	
	public void child(NodeList nodes,DefaultMutableTreeNode node)throws Exception{
		
		for(int j=0;j<nodes.getLength();j++){
			Node node2=nodes.item(j);
			
			
			if(node2!=null&&node2.getNodeType()==node2.TEXT_NODE&&node2.getNodeValue().trim().length()!=0){
				node22=new DefaultMutableTreeNode(node2.getNodeValue());
				node.add(node22);
			}
			if(node2!=null&&node2.getNodeType()==node2.ELEMENT_NODE){
				NamedNodeMap attributes2=node2.getAttributes();
				if(attributes2!=null){
				for(int k=0;k<attributes2.getLength();k++){
					if(attributes2.item(k)!=null){
						string1=string1+"   "+attributes2.item(k).toString().trim();
						node221=new DefaultMutableTreeNode(string1);
					}
					
				}
				}
				if(node221!=null){
				node22=new DefaultMutableTreeNode("<"+" "+node2.getNodeName()+"   "+node221+">");
				node.add(node22);
				node221=null;
				string1="";
				}
				else {
					node22=new DefaultMutableTreeNode("<"+" "+node2.getNodeName()+" "+">");
					node.add(node22);
				}
			}
			
			/*NamedNodeMap attributes2=node2.getAttributes();
			if(attributes2!=null){
			for(int k=0;k<attributes2.getLength();k++){
				if(attributes2.item(k)!=null){
					DefaultMutableTreeNode node221=new DefaultMutableTreeNode(attributes2.item(k));
					node22.add(node221);
				}
			}
			}*/
			NodeList nodes2=node2.getChildNodes();
			if(nodes2!=null){
				child(nodes2,node22);
			}
			
			if(node2!=null&&node2.getNodeType()==node2.ELEMENT_NODE){
				node22=new DefaultMutableTreeNode("<"+" /"+node2.getNodeName()+" "+">");
			node.add(node22);
			}
		}
		
	}
	
	public void go() {
		frame.setVisible(true);
	}
	
	public static void main(String args[]) {
		(new XmlTree()).go();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -