Overview
Woodstox provides excellent support for StAX (Streaming API for XML) parsing. Streaming API is used to parse large XML documents in a performance efficient manner. StAX compliant parsers do not load the entire XML document in memory but use a ‘Pull Model’ to load sections of document and parse it.
Woodstox is compliant with JSR-173 standard.
In the following program, we shall explore how to perform basic StAX parsing using Woodstox.
Create the sample XML file as shown below
1 Alba 100
Create the TestBasicStaxParsing parser as shown below.
Load the XML file as an
InputStream
(see line 15 below).
Create
XMLStreamReader2
using
XMLInputFactory
class (see lines 16-17 below).
Iterate through
XMLStreamReader2
using hasNext() method (see line 18 below).
Get the event type using
XMLStreamReader2
‘s next() method (see line 19 below).
Based on the type of the event, extract the contents and print them to console (see lines 20-33 below).
package com.studytrails.tutorials.woodstox.xmlstreamreader; import java.io.InputStream; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.events.XMLEvent; import org.codehaus.stax2.XMLInputFactory2; import org.codehaus.stax2.XMLStreamReader2; public class TestBasicStaxParsing { private void execute(String xmlFileName) throws Exception { InputStream xmlInputStream = getClass().getResourceAsStream(xmlFileName); XMLInputFactory2 xmlInputFactory = (XMLInputFactory2)XMLInputFactory.newInstance(); XMLStreamReader2 xmlStreamReader = (XMLStreamReader2) xmlInputFactory.createXMLStreamReader(xmlInputStream); while(xmlStreamReader.hasNext()){ int eventType = xmlStreamReader.next(); switch (eventType) { case XMLEvent.START_ELEMENT: System.out.print("<"+xmlStreamReader.getName().toString()+">"); break; case XMLEvent.CHARACTERS: System.out.print(xmlStreamReader.getText()); break; case XMLEvent.END_ELEMENT: System.out.println(""); break; default: //do nothing break; } } } public static void main(String[] args) throws Exception { (new TestBasicStaxParsing()).execute("employee.xml"); }
}
The output of the program demonstrating basic StAX parsing is shown below:
1 Alba 100