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






import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import javax.xml.stream.*;
import javax.xml.stream.events.*;
import java.util.Date;
import javax.xml.namespace.QName;


public class MyStreamFilter implements javax.xml.stream.StreamFilter {
    public static void main(String[] args) throws Exception {
        String filename = "yourXML.xml";

        XMLInputFactory xmlif = null;

        xmlif = XMLInputFactory.newInstance();
        xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,Boolean.TRUE);
        xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,Boolean.FALSE);
        xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
        xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

        System.out.println("FACTORY: " + xmlif);
        System.out.println("filename = " + filename);

        FileInputStream fis = new FileInputStream(filename);

        XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis),new MyStreamFilter());

        int eventType = xmlr.getEventType();
        printEventType(eventType);

        while (xmlr.hasNext()) {
                eventType = xmlr.next();
                printEventType(eventType);
                printName(xmlr, eventType);
                printText(xmlr);

                if (xmlr.isStartElement()) {
                    printAttributes(xmlr);
                }
                printPIData(xmlr);
        }
    }

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

        return "UNKNOWN_EVENT_TYPE";
    }

    private static void printEventType(int eventType) {
        System.out.print("EVENT TYPE(" + eventType + "):");
        System.out.println(getEventTypeString(eventType));
    }

    private static void printName(
        XMLStreamReader xmlr,
        int eventType) {
        if (xmlr.hasName()) {
            System.out.println("HAS NAME: " + xmlr.getLocalName());
        } else {
            System.out.println("HAS NO NAME");
        }
    }

    private static void printText(XMLStreamReader xmlr) {
        if (xmlr.hasText()) {
            System.out.println("HAS TEXT: " + xmlr.getText());
        } else {
            System.out.println("HAS NO TEXT");
        }
    }

    private static void printPIData(XMLStreamReader xmlr) {
        if (xmlr.getEventType() == XMLEvent.PROCESSING_INSTRUCTION) {
            System.out.println(" PI target = " + xmlr.getPITarget());
            System.out.println(" PI Data = " + xmlr.getPIData());
        }
    }

    private static void printAttributes(XMLStreamReader xmlr) {
        if (xmlr.getAttributeCount() > 0) {
            System.out.println("\nHAS ATTRIBUTES: ");

            int count = xmlr.getAttributeCount();

            for (int i = 0; i < count; i++) {
                QName name = xmlr.getAttributeName(i);
                String namespace = xmlr.getAttributeNamespace(i);
                String type = xmlr.getAttributeType(i);
                String prefix = xmlr.getAttributePrefix(i);
                String value = xmlr.getAttributeValue(i);

                System.out.println("ATTRIBUTE-PREFIX: " + prefix);
                System.out.println("ATTRIBUTE-NAMESP: " + namespace);
                System.out.println("ATTRIBUTE-NAME:   " + name.toString());
                System.out.println("ATTRIBUTE-VALUE:  " + value);
                System.out.println("ATTRIBUTE-TYPE:  " + type);
            }
        } else {
            System.out.println("HAS NO ATTRIBUTES");
        }
    }
    public boolean accept(XMLStreamReader reader) {
        if (!reader.isStartElement() && !reader.isEndElement()) {
            return false;
        } else {
            return true;
        }
    }
}








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