StAX - an Cursor Example

Příklad 5. StAX - an Cursor Example

import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class ParseByIterator {

    public static void main(String[] args) throws FileNotFoundException, XMLStreamException {
        // Use reference implementation 
        System.setProperty("javax.xml.stream.XMLInputFactory", "com.bea.xml.stream.MXParserFactory"); // Create an input factory
        XMLInputFactory xmlif = XMLInputFactory.newInstance(); // Create an XML stream reader X
        XMLStreamReader xmlr = xmlif.createXMLStreamReader(new FileReader("somefile.xml")); // Loop over XML input stream and process events 
        while (xmlr.hasNext()) {
            processEvent(xmlr);
            xmlr.next();
        }
    }

    /**
     * Process a single event
     *
     * @param xmlr - the XML stream reader
     */
    private static void processEvent(XMLStreamReader xmlr) {
        switch (xmlr.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                processName(xmlr);
                processAttributes(xmlr);
                break;
            case XMLStreamConstants.END_ELEMENT:
                processName(xmlr);
                break;
            case XMLStreamConstants.SPACE:
            case XMLStreamConstants.CHARACTERS:
                int start = xmlr.getTextStart();
                int length = xmlr.getTextLength();
                String text = new String(xmlr.getTextCharacters(), start, length);
                break;
            case XMLStreamConstants.COMMENT:
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                if (xmlr.hasText()) {
                    String piOrComment = xmlr.getText();
                }
                break;
        }
    }

    private static void processName(XMLStreamReader xmlr) {
        if (xmlr.hasName()) {
            String prefix = xmlr.getPrefix();
            String uri = xmlr.getNamespaceURI();
            String localName = xmlr.getLocalName();
        }
    }

    private static void processAttributes(XMLStreamReader xmlr) {
        for (int i = 0; i < xmlr.getAttributeCount(); i++) {
            processAttribute(xmlr, i);
        }
    }

    private static void processAttribute(XMLStreamReader xmlr, int index) {
        String prefix = xmlr.getAttributePrefix(index);
        String namespace = xmlr.getAttributeNamespace(index);
        QName localName = xmlr.getAttributeName(index);
        String value = xmlr.getAttributeValue(index);
    }
}

[Poznámka] Poznámka

Sample from Tip: Use XML streaming parsers (IBM DeveloperWorks, XML section).