Create a StAX Event Tree Viewer : StAX « XML « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » XML » StAXScreenshots 
Create a StAX Event Tree Viewer
   

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(800650);
    buildTree(defaultTreeModel, base, new File("yourXML.xml"));
    getContentPane().add(new JScrollPane(jTree), BorderLayout.CENTER);
    setVisible(true);
  }

  public static void main(String[] argsthrows 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 = (StartDocumentevent;
        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 = (StartElementevent;
        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 = (Attributeit.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 = (DefaultMutableTreeNodecurrent.getParent();
        break;
      case XMLStreamConstants.CHARACTERS:
        Characters characters = (Charactersevent;
        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 = (DTDevent;
        current.add(new DefaultMutableTreeNode(dtde.getDocumentTypeDeclaration()));
      default:
        System.out.println(event.getClass().getName());
      }
    }
  }
}

   
    
  
Related examples in the same category
1.Load XML document to a tree with XML stream reader
2.Validate Stax
3.StAX parser: The program prints all hyperlinks links of an XHTML web page
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.