Get information as an XMLEvent object when using cursor approach : Streaming XML Parser « JDK 6 « Java






Get information as an XMLEvent object when using cursor approach

 

import java.io.FileInputStream;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import javax.xml.stream.util.XMLEventAllocator;

import com.sun.xml.internal.stream.events.XMLEventAllocatorImpl;


class CursorApproachEventObject {
    static XMLEventAllocator allocator = null;

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

        XMLInputFactory xmlif = XMLInputFactory.newInstance();
        xmlif.setEventAllocator(new XMLEventAllocatorImpl());
        allocator = xmlif.getEventAllocator();

        XMLStreamReader xmlr = xmlif.createXMLStreamReader(filename,new FileInputStream(filename));

        int eventType = xmlr.getEventType();

        while (xmlr.hasNext()) {
            eventType = xmlr.next();
            if ((eventType == XMLStreamConstants.START_ELEMENT)&& xmlr.getLocalName().equals("Book")) {
                StartElement event = getXMLEvent(xmlr).asStartElement();
                System.out.println("EVENT: " + event.toString());
            }
        }
    }

    private static XMLEvent getXMLEvent(XMLStreamReader reader)
        throws XMLStreamException {
        return allocator.allocate(reader);
    }
}

        








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
8.Streaming XML Parser: Stax Event Filter
9.XMLEventReader and XMLEventWriter for Stax XML parser
10.Using XMLStreamWriter to create XML file
11.STAX cursor
12.Catch XMLStreamException