Java StAX: XMLInputFactory

Jakob Jenkov
Last update: 2024-02-13

The Java class javax.xml.stream.XMLInputFactory is one of the root component of the Java StAX API. From this class you can create both an XMLStreamReader and an XMLEventReader instance. The XMLStreamReader and XMLEventReader instances are used to parse XML using either a stream-of-tokens approach, or an event-based (callback based) approach. You can read more about these classes in Java Stax XMLEventReader and Java Stax XMLStreamReader.

Here are two examples showing how to create an XMLEventReader and an XMLStreamReader :

XMLInputFactory factory = XMLInputFactory.newInstance();

XMLEventReader eventReader =
    factory.createXMLEventReader(
        new FileReader("data\\test.xml"));

XMLStreamReader streamReader =
    factory.createXMLStreamReader(
        new FileReader("data\\test.xml"));

XMLInputFactory Properties

You can set various properties on the XMLInputFactory instance using the setProperty() method. Here is an example:

factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);

For a full list of properties and their meaning, see the official JavaDoc for the StAX API.

Jakob Jenkov

Featured Videos

Java ForkJoinPool

P2P Networks Introduction




















Advertisements

High-Performance
Java Persistence
Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next