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