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

📄 xmltreeviewer.java

📁 BEA WebLogic Server 8.1大全 = BEA webLogic server 8.1 unleashed (美) Mark Artiges等著 袁毅 ... [等] 译 eng
💻 JAVA
字号:
/* 
 * WebLogic Server Unleashed
 * 
 */


package com.wlsunleashed.xml.dom;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

import weblogic.apache.xerces.parsers.DOMParser;


/**
 * This class Represents a given XML document as a Tree.
 * 
 * @version 1.0
 */
public class XMLTreeViewer extends JFrame {
    /**
     * Creates a new DOMRepresentation object.
     * 
     * @param title The title of the application
     * @param xmlFile The XML file to be parsed
     * 
     * @throws IOException If an I/O error occurs
     * @throws SAXException If an exception occurs while parsing.
     */
    public XMLTreeViewer(String title, String xmlFile)
                  throws IOException, SAXException {
        super(title);
        setSize(400, 400);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(1, 1));

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

        JButton closeButton = new JButton();
        closeButton.setText("Close");
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(1);
            }
        });
        buttonPanel.add(closeButton);

        GridBagLayout gbl = new GridBagLayout();
        gbl.rowWeights = new double[] { 90, 10 };
        gbl.columnWeights = new double[] { 100 };
        getContentPane().setLayout(gbl);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridy = 0;
        getContentPane().add(mainPanel, gbc);
        gbc.gridy = 1;
        getContentPane().add(buttonPanel, gbc);

        Document theDoc = parseDocument(xmlFile);

        DefaultMutableTreeNode top = new DefaultMutableTreeNode("The XML");
        top.setAllowsChildren(true);

        populateTree(theDoc, top);

        JTree theTree = new JTree(top);
        JScrollPane aScrollPane = new JScrollPane(theTree);
        mainPanel.add(aScrollPane);
    }

    /**
     * The entry point into this application
     * 
     * @param args All the command line parameters
     */
    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Usage DOMRepresentation <Any XML file>.xml");
            System.exit(1);	
        }

        try {
            XMLTreeViewer anXMLTreeViewer = new XMLTreeViewer(
                                                    "Tree view of an XML document (Using DOM Parser)", 
                                                    args[0]);
            anXMLTreeViewer.setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Method parseDocument. Parses the document and displays it on a tree
     * 
     * @param xmlFile
     * 
     * @return Document
     * 
     * @throws IOException When an IO exception occurs
     * @throws SAXException When an error occurs while parsing.
     */
    private Document parseDocument(String xmlFile)
                            throws IOException, SAXException {
        DOMParser aParser = new DOMParser();


        aParser.parse(xmlFile);

        return aParser.getDocument();
    }

    /**
     * Method populateTree. Populates the tree with the given node. 
     * Recursive function.
     * 
     * @param theNode The node to populate
     * @param parent The parent tree node
     */
    private void populateTree(Node theNode, DefaultMutableTreeNode parent) {
        String str = "";

        switch (theNode.getNodeType()) {
        case Node.DOCUMENT_NODE:
            System.out.println("DOCUMENT_NODE");

            NodeList children = theNode.getChildNodes();

            for (int i = 0; i < children.getLength(); i++) {
                Node aNode = children.item(i);
                populateTree(aNode, parent);
            }

            break;

        case Node.ELEMENT_NODE:
            System.out.println("ELEMENT_NODE");
            str = "<" + theNode.getNodeName();

            NamedNodeMap attrList = theNode.getAttributes();

            for (int i = 0; i < attrList.getLength(); i++) {
                Node aNode = attrList.item(i);

                str += (" " + aNode.getNodeName() + "=\"" + aNode.getNodeValue() + "\"");
            }

            str += ">";

            DefaultMutableTreeNode newTreeNode = new DefaultMutableTreeNode(str, 
                                                                            theNode.hasChildNodes());

            children = theNode.getChildNodes();

            for (int i = 0; i < children.getLength(); i++) {
                populateTree(children.item(i), newTreeNode);
            }

            parent.add(newTreeNode);

            break;

        case Node.TEXT_NODE:
            System.out.println("TEXT_NODE");
            str = theNode.getNodeValue();

            if (str.trim().length() == 0) {
                break;
            }

            newTreeNode = new DefaultMutableTreeNode(str, false);
            parent.add(newTreeNode);

            break;

        case Node.CDATA_SECTION_NODE:
            System.out.println("CDATA_SECTION_NODE");
            str = "<![CDATA[" + theNode.getNodeValue() + "]]>";
            newTreeNode = new DefaultMutableTreeNode(str, false);
            parent.add(newTreeNode);

            break;

        case Node.COMMENT_NODE:
            System.out.println("COMMENT_NODE");
            str = "<!--" + theNode.getNodeValue() + "-->";
            newTreeNode = new DefaultMutableTreeNode(str, false);
            parent.add(newTreeNode);

            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            System.out.println("PROCESSING_INSTRUCTION_NODE");

            break;

        case Node.ENTITY_REFERENCE_NODE:
            System.out.println("ENTITY_REFERENCE_NODE");

            break;

        case Node.DOCUMENT_TYPE_NODE:
            System.out.println("DOCUMENT_TYPE_NODE");

            DocumentType docType = (DocumentType) theNode;
            str = "<!DOCTYPE ";

            if (docType.getPublicId() != null) {
                str += (" PUBLIC \"" + docType.getPublicId() + "\" ");
            }

            if (docType.getSystemId() != null) {
                str += (" SYSTEM \"" + docType.getSystemId() + "\" ");
            }

            str += ">";
            newTreeNode = new DefaultMutableTreeNode(str, false);
            parent.add(newTreeNode);

            break;
        }

        //return top;
    }
}

⌨️ 快捷键说明

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