XMLEventReader and XMLEventWriter for Stax XML parser : Streaming XML Parser « JDK 6 « Java

XMLEventReader and XMLEventWriter for Stax XML parser


import java.util.Calendar;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.XMLEvent;

class StaxEvent {
    XMLEventFactory m_eventFactory = XMLEventFactory.newInstance();

    public static void main(String[] args) throws Exception {
            StaxEvent ms = new StaxEvent();

            XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(new java.io.FileInputStream("yourXML.xml"));
            XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);

            while (reader.hasNext()) {
                XMLEvent event = (XMLEvent) reader.next();

                if (event.getEventType() == event.CHARACTERS) {
                } else {

    private Characters getNewCharactersEvent(Characters event) {
        if (event.getData().equalsIgnoreCase("Name1")) {
            return m_eventFactory.createCharacters(Calendar.getInstance().getTime().toString());
        } else {
            return event;


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.Using XMLStreamWriter to create XML file
10.STAX cursor
11.Catch XMLStreamException
12.Get information as an XMLEvent object when using cursor approach