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

📄 xmlframe.java

📁 nanjing university cs 的java课件。 对新手很有用。付课件中源码。
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class XMLFrame extends JFrame {
	public static void main(String[] args) {
		String filename;
		if (args.length > 0) {
			filename = args[0];
		}
		else {
			filename = "test.xml";
		}
		new XMLFrame(filename);
	}

	public XMLFrame(String filename) {
		try {
			JTree tree = new XMLTree(filename);
			JFrame frame = new JFrame(filename);
			frame.addWindowListener(new WindowAdapter(){
	    	    public void windowClosing(WindowEvent e){
			    	System.exit(0);
		    	}
    		});
			Container content = frame.getContentPane();
			content.add(new JScrollPane(tree));
			frame.pack();
			frame.setVisible(true);
		}
		catch (IOException ioe) {
			System.out.println("Error creating tree: " + ioe);
		}
	}
}


/** Given a filename or a name and an input stream,
 * this class generates a JTree representing the
 * XML structure contained in the file or stream.
 * Parses with DOM then copies the tree structure
 * (minus text and comment nodes).
 */
class XMLTree extends JTree {
	public XMLTree(String filename) throws IOException {
		this(filename, new FileInputStream(new File(filename)));
	}

	public XMLTree(String filename, InputStream in) {
		super(makeRootNode(in));
	}

	private static DefaultMutableTreeNode makeRootNode(InputStream in) {
		try {
		// Use the system property
		// javax.xml.parsers.DocumentBuilderFactory (set either
		// from Java code or by using the -D option to "java").
			DocumentBuilderFactory builderFactory =
				DocumentBuilderFactory.newInstance();
			DocumentBuilder builder =
				builderFactory.newDocumentBuilder();
			Document document = builder.parse(in);
			document.getDocumentElement().normalize();
			Element rootElement = document.getDocumentElement();
			DefaultMutableTreeNode rootTreeNode =
				buildTree(rootElement);
			return (rootTreeNode);
		}
		catch (Exception e) {
			String errorMessage = "Error making root node: " + e;
			System.err.println(errorMessage);
			e.printStackTrace();
			return (new DefaultMutableTreeNode(errorMessage));
		}
	}

	private static DefaultMutableTreeNode buildTree(Element rootElement) {
		// Make a JTree node for the root, then make JTree
		// nodes for each child and add them to the root node.
		// The addChildren method is recursive.
		DefaultMutableTreeNode rootTreeNode =
			new DefaultMutableTreeNode(treeNodeLabel(rootElement));
		addChildren(rootTreeNode, rootElement);
		return (rootTreeNode);
	}

	private static void addChildren(DefaultMutableTreeNode parentTreeNode, Node parentXMLElement) {
		// Recursive method that finds all the child elements and adds
		// them to the parent node. Nodes corresponding to the graphical
		// JTree will have the word "tree" in the variable name.
		NodeList childElements =
			parentXMLElement.getChildNodes();
		for (int i = 0; i < childElements.getLength(); i++) {
			Node childElement = childElements.item(i);
			if (! (childElement instanceof Text ||
				   childElement instanceof Comment)) {
				DefaultMutableTreeNode childTreeNode =
					new DefaultMutableTreeNode
					(treeNodeLabel(childElement));
				parentTreeNode.add(childTreeNode);
				addChildren(childTreeNode, childElement);
			}
		}
	}

	private static String treeNodeLabel(Node childElement) {
		NamedNodeMap elementAttributes =
			childElement.getAttributes();
		String treeNodeLabel = childElement.getNodeName();
		if (elementAttributes != null &&
			elementAttributes.getLength() > 0) {
			treeNodeLabel = treeNodeLabel + " (";
			int numAttributes = elementAttributes.getLength();
			for (int i = 0; i < numAttributes; i++) {
				Node attribute = elementAttributes.item(i);
				if (i > 0) {
					treeNodeLabel = treeNodeLabel + ", ";
				}
				treeNodeLabel =
					treeNodeLabel + attribute.getNodeName() +
					"=" + attribute.getNodeValue();
			}
			treeNodeLabel = treeNodeLabel + ")";
		}
		return (treeNodeLabel);
	}
}

⌨️ 快捷键说明

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