Streaming XML Parser: Stax Event : Streaming XML Parser « JDK 6 « Java






Streaming XML Parser: Stax Event

 
import java.io.FileInputStream;
import javax.xml.stream.*;
import javax.xml.stream.events.*;
import javax.xml.namespace.QName;


public class EventParse {
    public static void main(String[] args) throws Exception {
        String filename = "yourXML.xml";

        XMLInputFactory factory = XMLInputFactory.newInstance();
        System.out.println("FACTORY: " + factory);

        XMLEventReader r = factory.createXMLEventReader(filename,new FileInputStream(filename));

        while (r.hasNext()) {
            XMLEvent e = r.nextEvent();
            System.out.println(e.toString());
        }
    }

    public static final String getEventTypeString(int eventType) {
        switch (eventType) {
        case XMLEvent.START_ELEMENT:
            return "START_ELEMENT";

        case XMLEvent.END_ELEMENT:
            return "END_ELEMENT";

        case XMLEvent.PROCESSING_INSTRUCTION:
            return "PROCESSING_INSTRUCTION";

        case XMLEvent.CHARACTERS:
            return "CHARACTERS";

        case XMLEvent.COMMENT:
            return "COMMENT";

        case XMLEvent.START_DOCUMENT:
            return "START_DOCUMENT";

        case XMLEvent.END_DOCUMENT:
            return "END_DOCUMENT";

        case XMLEvent.ENTITY_REFERENCE:
            return "ENTITY_REFERENCE";

        case XMLEvent.ATTRIBUTE:
            return "ATTRIBUTE";

        case XMLEvent.DTD:
            return "DTD";

        case XMLEvent.CDATA:
            return "CDATA";

        case XMLEvent.SPACE:
            return "SPACE";
        }

        return "UNKNOWN_EVENT_TYPE " + "," + eventType;
    }
}

        








Related examples in the same category

1.Stax XML iterator read
2.Stax XML cursor read
3.StAX is a technology similar to SAX.
4.XMLEventReader Demo
5.XMLStreamWriter Demo
6.Using XMLEventFactory to create xml document
7.Streaming XML Parser: Stax Event Filter
8.XMLEventReader and XMLEventWriter for Stax XML parser
9.Using XMLStreamWriter to create XML file
10.STAX cursor
11.Catch XMLStreamException
12.Get information as an XMLEvent object when using cursor approach