StAXEventTreeViewer.java Source code

Java tutorial

Introduction

Here is the source code for StAXEventTreeViewer.java

Source

import java.awt.BorderLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.DTD;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class StAXEventTreeViewer extends JFrame {
    DefaultMutableTreeNode base = new DefaultMutableTreeNode("XML File");
    DefaultTreeModel defaultTreeModel = new DefaultTreeModel(base);
    private JTree jTree = new JTree(defaultTreeModel);

    public StAXEventTreeViewer() throws XMLStreamException, FileNotFoundException {
        setSize(800, 650);
        buildTree(defaultTreeModel, base, new File("yourXML.xml"));
        getContentPane().add(new JScrollPane(jTree), BorderLayout.CENTER);
        setVisible(true);
    }

    public static void main(String[] args) throws Exception {
        new StAXEventTreeViewer();
    }

    public void buildTree(DefaultTreeModel treeModel, DefaultMutableTreeNode current, File file)
            throws XMLStreamException, FileNotFoundException {

        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(file));
        while (reader.hasNext()) {
            XMLEvent event = reader.nextEvent();
            switch (event.getEventType()) {
            case XMLStreamConstants.START_DOCUMENT:
                StartDocument startDocument = (StartDocument) event;
                DefaultMutableTreeNode version = new DefaultMutableTreeNode(startDocument.getVersion());
                current.add(version);

                current.add(new DefaultMutableTreeNode(startDocument.isStandalone()));
                current.add(new DefaultMutableTreeNode(startDocument.standaloneSet()));
                current.add(new DefaultMutableTreeNode(startDocument.encodingSet()));
                current.add(new DefaultMutableTreeNode(startDocument.getCharacterEncodingScheme()));
                break;
            case XMLStreamConstants.START_ELEMENT:
                StartElement startElement = (StartElement) event;
                QName elementName = startElement.getName();

                DefaultMutableTreeNode element = new DefaultMutableTreeNode(elementName.getLocalPart());
                current.add(element);
                current = element;

                if (!elementName.getNamespaceURI().equals("")) {
                    String prefix = elementName.getPrefix();
                    if (prefix.equals("")) {
                        prefix = "[None]";
                    }
                    DefaultMutableTreeNode namespace = new DefaultMutableTreeNode(
                            "prefix=" + prefix + ",URI=" + elementName.getNamespaceURI());
                    current.add(namespace);
                }

                for (Iterator it = startElement.getAttributes(); it.hasNext();) {
                    Attribute attr = (Attribute) it.next();
                    DefaultMutableTreeNode attribute = new DefaultMutableTreeNode("Attribute (name="
                            + attr.getName().getLocalPart() + ",value=" + attr.getValue() + "')");
                    String attURI = attr.getName().getNamespaceURI();
                    if (!attURI.equals("")) {
                        String attPrefix = attr.getName().getPrefix();
                        if (attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        attribute.add(new DefaultMutableTreeNode("prefix = " + attPrefix + ", URI = " + attURI));
                    }
                    current.add(attribute);
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                current = (DefaultMutableTreeNode) current.getParent();
                break;
            case XMLStreamConstants.CHARACTERS:
                Characters characters = (Characters) event;
                if (!characters.isIgnorableWhiteSpace() && !characters.isWhiteSpace()) {
                    String data = characters.getData();
                    if (data.length() != 0) {
                        current.add(new DefaultMutableTreeNode(characters.getData()));
                    }
                }
                break;
            case XMLStreamConstants.DTD:
                DTD dtde = (DTD) event;
                current.add(new DefaultMutableTreeNode(dtde.getDocumentTypeDeclaration()));
            default:
                System.out.println(event.getClass().getName());
            }
        }
    }
}