Java Reference
In-Depth Information
Parsing XML Documents
Document readers are obtained by calling the various “
create
” methods that are
declared in the
javax.xml.stream.XMLInputFactory
class. These creational
methods are organized into two categories: methods for creating stream-based readers
and methods for creating event-based readers.
Beforeyoucanobtainastream-basedoranevent-basedreader,youneedtoobtainan
instance of the factory by calling one of the
newFactory()
class methods, such as
XMLInputFactory newFactory()
:
XMLInputFactory xmlif = XMLInputFactory.newFactory();
Note
Youcanalsocallthe
XMLInputFactory newInstance()
classmethod
but might not wish to do so because its same-named but parameterized companion
method has been deprecated to maintain API consistency, and it is probable that
newInstance()
will be deprecated as well.
The
newFactory()
methods follow an ordered lookup procedure to locate the
XMLInputFactory
implementation class. This procedure first examines the
javax.xml.stream.XMLInputFactory
systemproperty,andlastlychoosesthe
nameoftheJavaplatform'sdefault
XMLInputFactory
implementationclass.Ifthis
procedure cannot find a classname, or if the class cannot be loaded (or instantiated),
the
method
throws
an
instance
of
the
javax.xml.stream.FactoryConfigurationError
class.
After creating the factory, call
XMLInputFactory
's
void setProp-
erty(String name, Object value)
method to set various features and
properties as necessary. For example, you might execute
xm-
lif.setProperty(XMLInputFactory.IS_VALIDATING, true);
(
true
DTD-validating stream-based reader. However, the default StAX factory implement-
ation throws
IllegalArgumentException
because it doesn't support DTD val-
idation. Similarly, you might execute
xm-
lif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
to request a namespace-aware event-based reader, which is supported.
Parsing Documents with Stream-Based Readers
A stream-based reader is created by calling one of
XMLInputFactory
's
cre-
ateXMLStreamReader()
methods, such as
XMLStreamReader
cre-