Top Description Fields Constructors Methods
org.springframework.util.xml

public abstract Class StaxUtils

extends Object
Class Inheritance
Imports
java.io.InputStream, java.util.List, java.util.function.Supplier, javax.xml.stream.XMLEventFactory, .XMLEventReader, .XMLEventWriter, .XMLInputFactory, .XMLResolver, .XMLStreamException, .XMLStreamReader, .XMLStreamWriter, javax.xml.stream.events.XMLEvent, javax.xml.transform.Result, .Source, javax.xml.transform.stax.StAXResult, .StAXSource, org.xml.sax.ContentHandler, .XMLReader, org.springframework.lang.Nullable

Convenience methods for working with the StAX API. Partly historic due to JAXP 1.3 compatibility; as of Spring 4.0, relying on JAXP 1.4 as included in JDK 1.6 and higher.

In particular, methods for using StAX (javax.xml.stream) in combination with the TrAX API (javax.xml.transform), and converting StAX readers/writers into SAX readers/handlers and vice-versa.

Authors
Arjen Poutsma, Juergen Hoeller
Since
3.0

Field Summary

Modifier and TypeField and Description
private static final XMLResolver

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static ContentHandler

Returns:

a content handler writing to the streamWriter
createContentHandler
(XMLStreamWriter
the StAX stream writer
streamWriter
)

Create a SAX ContentHandler that writes to the given StAX XMLStreamWriter.

public static ContentHandler

Returns:

a content handler writing to the eventWriter
createContentHandler
(XMLEventWriter
the StAX event writer
eventWriter
)

Create a SAX ContentHandler that writes events to the given StAX XMLEventWriter.

public static Result

Returns:

a source wrapping the streamWriter
createCustomStaxResult
(XMLStreamWriter
the StAX stream writer
streamWriter
)

Create a custom, non-JAXP 1.4 StAX Result for the given XMLStreamWriter.

public static Result

Returns:

a source wrapping the eventWriter
createCustomStaxResult
(XMLEventWriter
the StAX event writer
eventWriter
)

Create a custom, non-JAXP 1.4 StAX Result for the given XMLEventWriter.

public static Source

Returns:

a source wrapping the streamReader
createCustomStaxSource
(XMLStreamReader
the StAX stream reader
streamReader
)

Create a custom, non-JAXP 1.4 StAX Source for the given XMLStreamReader.

public static Source

Returns:

a source wrapping the eventReader
createCustomStaxSource
(XMLEventReader
the StAX event reader
eventReader
)

Create a custom, non-JAXP 1.4 StAX Source for the given XMLEventReader.

public static XMLInputFactory

Returns:

a new defensively initialized input factory instance to use
createDefensiveInputFactory
()

Create an XMLInputFactory with Spring's defensive setup, i.e. no support for the resolution of DTDs and external entities.

public static <T extends XMLInputFactory> T

Returns:

a new defensively initialized input factory instance to use
createDefensiveInputFactory
(Supplier<T>
supplier for the input factory instance
instanceSupplier
)

Variant of createDefensiveInputFactory() with a custom instance.

public static XMLStreamReader

Returns:

a stream reader that reads from an event reader
createEventStreamReader
(XMLEventReader eventReader)

Return a XMLStreamReader that reads from a XMLEventReader.

public static XMLStreamWriter

Returns:

a stream writer that writes to an event writer
createEventStreamWriter
(XMLEventWriter eventWriter)

Return a XMLStreamWriter that writes to a XMLEventWriter.

public static XMLStreamWriter

Returns:

a stream writer that writes to an event writer
createEventStreamWriter
(XMLEventWriter eventWriter, XMLEventFactory eventFactory)

Return a XMLStreamWriter that writes to a XMLEventWriter.

public static Result

Returns:

a result wrapping the streamWriter
createStaxResult
(XMLStreamWriter
the StAX stream writer
streamWriter
)

Create a JAXP 1.4 StAXResult for the given XMLStreamWriter.

public static Result

Returns:

a result wrapping streamReader
createStaxResult
(XMLEventWriter
the StAX event writer
eventWriter
)

Create a JAXP 1.4 StAXResult for the given XMLEventWriter.

public static Source

Returns:

a source wrapping the streamReader
createStaxSource
(XMLStreamReader
the StAX stream reader
streamReader
)

Create a JAXP 1.4 StAXSource for the given XMLStreamReader.

public static Source

Returns:

a source wrapping the eventReader
createStaxSource
(XMLEventReader
the StAX event reader
eventReader
)

Create a JAXP 1.4 StAXSource for the given XMLEventReader.

public static XMLEventReader

Returns:

an XMLEventReader that reads from the given events
createXMLEventReader
(List<XMLEvent>
the list of XMLEvents.
events
)

Create a XMLEventReader from the given list of XMLEvent.

public static XMLReader

Returns:

a XMLReader reading from the streamWriter
createXMLReader
(XMLStreamReader
the StAX stream reader
streamReader
)

Create a SAX XMLReader that reads from the given StAX XMLStreamReader.

public static XMLReader

Returns:

a XMLReader reading from the eventWriter
createXMLReader
(XMLEventReader
the StAX event reader
eventReader
)

Create a SAX XMLReader that reads from the given StAX XMLEventReader.

public static XMLEventReader

Returns:

the XMLEventReader
getXMLEventReader
(Source
a JAXP 1.4 StAXSource
source
)

Return the XMLEventReader for the given StAX Source.

public static XMLEventWriter

Returns:

the XMLStreamReader
getXMLEventWriter
(Result
a JAXP 1.4 StAXResult
result
)

Return the XMLEventWriter for the given StAX Result.

public static XMLStreamReader

Returns:

the XMLStreamReader
getXMLStreamReader
(Source
a JAXP 1.4 StAXSource
source
)

Return the XMLStreamReader for the given StAX Source.

public static XMLStreamWriter

Returns:

the XMLStreamReader
getXMLStreamWriter
(Result
a JAXP 1.4 StAXResult
result
)

Return the XMLStreamWriter for the given StAX Result.

public static boolean

Returns:

true if result is a JAXP 1.4 StAXResult or custom StAX Result; false otherwise
isStaxResult
(Result result)

Indicate whether the given Result is a JAXP 1.4 StAX Result or custom StAX Result.

public static boolean

Returns:

true if source is a JAXP 1.4 StAXSource or custom StAX Source; false otherwise
isStaxSource
(Source source)

Indicate whether the given Source is a JAXP 1.4 StAX Source or custom StAX Source.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

NO_OP_XML_RESOLVERback to summary
private static final XMLResolver NO_OP_XML_RESOLVER

Constructor Detail

StaxUtilsback to summary
public StaxUtils()

Method Detail

createContentHandlerback to summary
public static ContentHandler createContentHandler(XMLStreamWriter streamWriter)

Create a SAX ContentHandler that writes to the given StAX XMLStreamWriter.

Parameters
streamWriter:XMLStreamWriter

the StAX stream writer

Returns:ContentHandler

a content handler writing to the streamWriter

createContentHandlerback to summary
public static ContentHandler createContentHandler(XMLEventWriter eventWriter)

Create a SAX ContentHandler that writes events to the given StAX XMLEventWriter.

Parameters
eventWriter:XMLEventWriter

the StAX event writer

Returns:ContentHandler

a content handler writing to the eventWriter

createCustomStaxResultback to summary
public static Result createCustomStaxResult(XMLStreamWriter streamWriter)

Create a custom, non-JAXP 1.4 StAX Result for the given XMLStreamWriter.

Parameters
streamWriter:XMLStreamWriter

the StAX stream writer

Returns:Result

a source wrapping the streamWriter

createCustomStaxResultback to summary
public static Result createCustomStaxResult(XMLEventWriter eventWriter)

Create a custom, non-JAXP 1.4 StAX Result for the given XMLEventWriter.

Parameters
eventWriter:XMLEventWriter

the StAX event writer

Returns:Result

a source wrapping the eventWriter

createCustomStaxSourceback to summary
public static Source createCustomStaxSource(XMLStreamReader streamReader)

Create a custom, non-JAXP 1.4 StAX Source for the given XMLStreamReader.

Parameters
streamReader:XMLStreamReader

the StAX stream reader

Returns:Source

a source wrapping the streamReader

createCustomStaxSourceback to summary
public static Source createCustomStaxSource(XMLEventReader eventReader)

Create a custom, non-JAXP 1.4 StAX Source for the given XMLEventReader.

Parameters
eventReader:XMLEventReader

the StAX event reader

Returns:Source

a source wrapping the eventReader

createDefensiveInputFactoryback to summary
public static XMLInputFactory createDefensiveInputFactory()

Create an XMLInputFactory with Spring's defensive setup, i.e. no support for the resolution of DTDs and external entities.

Returns:XMLInputFactory

a new defensively initialized input factory instance to use

Since
5.0
createDefensiveInputFactoryback to summary
public static <T extends XMLInputFactory> T createDefensiveInputFactory(Supplier<T> instanceSupplier)

Variant of createDefensiveInputFactory() with a custom instance.

Parameters
instanceSupplier:Supplier<T>

supplier for the input factory instance

Returns:T

a new defensively initialized input factory instance to use

Since
5.0.12
createEventStreamReaderback to summary
public static XMLStreamReader createEventStreamReader(XMLEventReader eventReader) throws XMLStreamException

Return a XMLStreamReader that reads from a XMLEventReader. Useful because the StAX XMLInputFactory allows one to create an event reader from a stream reader, but not vice-versa.

Returns:XMLStreamReader

a stream reader that reads from an event reader

createEventStreamWriterback to summary
public static XMLStreamWriter createEventStreamWriter(XMLEventWriter eventWriter)

Return a XMLStreamWriter that writes to a XMLEventWriter.

Returns:XMLStreamWriter

a stream writer that writes to an event writer

Since
3.2
createEventStreamWriterback to summary
public static XMLStreamWriter createEventStreamWriter(XMLEventWriter eventWriter, XMLEventFactory eventFactory)

Return a XMLStreamWriter that writes to a XMLEventWriter.

Returns:XMLStreamWriter

a stream writer that writes to an event writer

Since
3.0.5
createStaxResultback to summary
public static Result createStaxResult(XMLStreamWriter streamWriter)

Create a JAXP 1.4 StAXResult for the given XMLStreamWriter.

Parameters
streamWriter:XMLStreamWriter

the StAX stream writer

Returns:Result

a result wrapping the streamWriter

createStaxResultback to summary
public static Result createStaxResult(XMLEventWriter eventWriter)

Create a JAXP 1.4 StAXResult for the given XMLEventWriter.

Parameters
eventWriter:XMLEventWriter

the StAX event writer

Returns:Result

a result wrapping streamReader

createStaxSourceback to summary
public static Source createStaxSource(XMLStreamReader streamReader)

Create a JAXP 1.4 StAXSource for the given XMLStreamReader.

Parameters
streamReader:XMLStreamReader

the StAX stream reader

Returns:Source

a source wrapping the streamReader

createStaxSourceback to summary
public static Source createStaxSource(XMLEventReader eventReader) throws XMLStreamException

Create a JAXP 1.4 StAXSource for the given XMLEventReader.

Parameters
eventReader:XMLEventReader

the StAX event reader

Returns:Source

a source wrapping the eventReader

createXMLEventReaderback to summary
public static XMLEventReader createXMLEventReader(List<XMLEvent> events)

Create a XMLEventReader from the given list of XMLEvent.

Parameters
events:List<XMLEvent>

the list of XMLEvents.

Returns:XMLEventReader

an XMLEventReader that reads from the given events

Since
5.0
createXMLReaderback to summary
public static XMLReader createXMLReader(XMLStreamReader streamReader)

Create a SAX XMLReader that reads from the given StAX XMLStreamReader.

Parameters
streamReader:XMLStreamReader

the StAX stream reader

Returns:XMLReader

a XMLReader reading from the streamWriter

createXMLReaderback to summary
public static XMLReader createXMLReader(XMLEventReader eventReader)

Create a SAX XMLReader that reads from the given StAX XMLEventReader.

Parameters
eventReader:XMLEventReader

the StAX event reader

Returns:XMLReader

a XMLReader reading from the eventWriter

getXMLEventReaderback to summary
public static XMLEventReader getXMLEventReader(Source source)

Return the XMLEventReader for the given StAX Source.

Parameters
source:Source

a JAXP 1.4 StAXSource

Returns:XMLEventReader

the XMLEventReader

Annotations
@Nullable
Exceptions
IllegalArgumentException:
if source isn't a JAXP 1.4 StAXSource or custom StAX Source
getXMLEventWriterback to summary
public static XMLEventWriter getXMLEventWriter(Result result)

Return the XMLEventWriter for the given StAX Result.

Parameters
result:Result

a JAXP 1.4 StAXResult

Returns:XMLEventWriter

the XMLStreamReader

Annotations
@Nullable
Exceptions
IllegalArgumentException:
if source isn't a JAXP 1.4 StAXResult or custom StAX Result
getXMLStreamReaderback to summary
public static XMLStreamReader getXMLStreamReader(Source source)

Return the XMLStreamReader for the given StAX Source.

Parameters
source:Source

a JAXP 1.4 StAXSource

Returns:XMLStreamReader

the XMLStreamReader

Annotations
@Nullable
Exceptions
IllegalArgumentException:
if source isn't a JAXP 1.4 StAXSource or custom StAX Source
getXMLStreamWriterback to summary
public static XMLStreamWriter getXMLStreamWriter(Result result)

Return the XMLStreamWriter for the given StAX Result.

Parameters
result:Result

a JAXP 1.4 StAXResult

Returns:XMLStreamWriter

the XMLStreamReader

Annotations
@Nullable
Exceptions
IllegalArgumentException:
if source isn't a JAXP 1.4 StAXResult or custom StAX Result
isStaxResultback to summary
public static boolean isStaxResult(Result result)

Indicate whether the given Result is a JAXP 1.4 StAX Result or custom StAX Result.

Returns:boolean

true if result is a JAXP 1.4 StAXResult or custom StAX Result; false otherwise

isStaxSourceback to summary
public static boolean isStaxSource(Source source)

Indicate whether the given Source is a JAXP 1.4 StAX Source or custom StAX Source.

Returns:boolean

true if source is a JAXP 1.4 StAXSource or custom StAX Source; false otherwise