Streaming XML Parser: Stax Event : Stream Parser « XML « Java Tutorial






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;
    }
}








33.8.Stream Parser
33.8.1.StAX is a technology similar to SAX.
33.8.2.XMLEventReader Demo
33.8.3.XMLStreamWriter Demo
33.8.4.Using XMLEventFactory to create xml document
33.8.5.Streaming XML Parser: Stax Event
33.8.6.Streaming XML Parser: Stax Event Filter
33.8.7.XMLEventReader and XMLEventWriter for Stax XML parser
33.8.8.Using XMLStreamWriter to create XML file
33.8.9.STAX cursor
33.8.10.Catch XMLStreamException
33.8.11.Get information as an XMLEvent object when using cursor approach
33.8.12.Stax XML iterator read
33.8.13.Stax XML cursor read
33.8.14.Read Xml from StreamSource
33.8.15.Validate Stax