
XML and Java • 373
7.2.4 JAXP and StAX
The StAX API is straightforward to use. We shall classify the usage of the API based on two requirements:
(a) creating an XML document and writing to it, and (b) reading it.
Creating an XML document
There are three important classes that we need to import at the beginning of the program:
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.XMLStreamException;
We then need to create an instance of the XMLOutputFactory class by using the standard factory technique:
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
Once we have obtained an instance ...