Package org.springframework.util.xml
Class StaxUtils
java.lang.Object
org.springframework.util.xml.StaxUtils
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.
- Since:
- 3.0
- Author:
- Arjen Poutsma, Juergen Hoeller
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic ContentHandler
createContentHandler
(XMLEventWriter eventWriter) Create a SAXContentHandler
that writes events to the given StAXXMLEventWriter
.static ContentHandler
createContentHandler
(XMLStreamWriter streamWriter) Create a SAXContentHandler
that writes to the given StAXXMLStreamWriter
.static Result
createCustomStaxResult
(XMLEventWriter eventWriter) Create a custom, non-JAXP 1.4 StAXResult
for the givenXMLEventWriter
.static Result
createCustomStaxResult
(XMLStreamWriter streamWriter) Create a custom, non-JAXP 1.4 StAXResult
for the givenXMLStreamWriter
.static Source
createCustomStaxSource
(XMLEventReader eventReader) Create a custom, non-JAXP 1.4 StAXSource
for the givenXMLEventReader
.static Source
createCustomStaxSource
(XMLStreamReader streamReader) Create a custom, non-JAXP 1.4 StAXSource
for the givenXMLStreamReader
.static XMLInputFactory
Create anXMLInputFactory
with Spring's defensive setup, i.e.static <T extends XMLInputFactory>
TcreateDefensiveInputFactory
(Supplier<T> instanceSupplier) Variant ofcreateDefensiveInputFactory()
with a custom instance.static XMLStreamReader
createEventStreamReader
(XMLEventReader eventReader) Return aXMLStreamReader
that reads from aXMLEventReader
.static XMLStreamWriter
createEventStreamWriter
(XMLEventWriter eventWriter) Return aXMLStreamWriter
that writes to aXMLEventWriter
.static XMLStreamWriter
createEventStreamWriter
(XMLEventWriter eventWriter, XMLEventFactory eventFactory) Return aXMLStreamWriter
that writes to aXMLEventWriter
.static Result
createStaxResult
(XMLEventWriter eventWriter) Create a JAXP 1.4StAXResult
for the givenXMLEventWriter
.static Result
createStaxResult
(XMLStreamWriter streamWriter) Create a JAXP 1.4StAXResult
for the givenXMLStreamWriter
.static Source
createStaxSource
(XMLEventReader eventReader) Create a JAXP 1.4StAXSource
for the givenXMLEventReader
.static Source
createStaxSource
(XMLStreamReader streamReader) Create a JAXP 1.4StAXSource
for the givenXMLStreamReader
.static XMLEventReader
createXMLEventReader
(List<XMLEvent> events) Create aXMLEventReader
from the given list ofXMLEvent
.static XMLReader
createXMLReader
(XMLEventReader eventReader) Create a SAXXMLReader
that reads from the given StAXXMLEventReader
.static XMLReader
createXMLReader
(XMLStreamReader streamReader) Create a SAXXMLReader
that reads from the given StAXXMLStreamReader
.static XMLEventReader
getXMLEventReader
(Source source) Return theXMLEventReader
for the given StAX Source.static XMLEventWriter
getXMLEventWriter
(Result result) Return theXMLEventWriter
for the given StAX Result.static XMLStreamReader
getXMLStreamReader
(Source source) Return theXMLStreamReader
for the given StAX Source.static XMLStreamWriter
getXMLStreamWriter
(Result result) Return theXMLStreamWriter
for the given StAX Result.static boolean
isStaxResult
(Result result) Indicate whether the givenResult
is a JAXP 1.4 StAX Result or custom StAX Result.static boolean
isStaxSource
(Source source) Indicate whether the givenSource
is a JAXP 1.4 StAX Source or custom StAX Source.
-
Constructor Details
-
StaxUtils
public StaxUtils()
-
-
Method Details
-
createDefensiveInputFactory
Create anXMLInputFactory
with Spring's defensive setup, i.e. no support for the resolution of DTDs and external entities.- Returns:
- a new defensively initialized input factory instance to use
- Since:
- 5.0
-
createDefensiveInputFactory
public static <T extends XMLInputFactory> T createDefensiveInputFactory(Supplier<T> instanceSupplier) Variant ofcreateDefensiveInputFactory()
with a custom instance.- Parameters:
instanceSupplier
- supplier for the input factory instance- Returns:
- a new defensively initialized input factory instance to use
- Since:
- 5.0.12
-
createStaxSource
Create a JAXP 1.4StAXSource
for the givenXMLStreamReader
.- Parameters:
streamReader
- the StAX stream reader- Returns:
- a source wrapping the
streamReader
-
createStaxSource
Create a JAXP 1.4StAXSource
for the givenXMLEventReader
.- Parameters:
eventReader
- the StAX event reader- Returns:
- a source wrapping the
eventReader
- Throws:
XMLStreamException
-
createCustomStaxSource
Create a custom, non-JAXP 1.4 StAXSource
for the givenXMLStreamReader
.- Parameters:
streamReader
- the StAX stream reader- Returns:
- a source wrapping the
streamReader
-
createCustomStaxSource
Create a custom, non-JAXP 1.4 StAXSource
for the givenXMLEventReader
.- Parameters:
eventReader
- the StAX event reader- Returns:
- a source wrapping the
eventReader
-
isStaxSource
Indicate whether the givenSource
is a JAXP 1.4 StAX Source or custom StAX Source.- Returns:
true
ifsource
is a JAXP 1.4StAXSource
or custom StAX Source;false
otherwise
-
getXMLStreamReader
Return theXMLStreamReader
for the given StAX Source.- Parameters:
source
- a JAXP 1.4StAXSource
- Returns:
- the
XMLStreamReader
- Throws:
IllegalArgumentException
- ifsource
isn't a JAXP 1.4StAXSource
or custom StAX Source
-
getXMLEventReader
Return theXMLEventReader
for the given StAX Source.- Parameters:
source
- a JAXP 1.4StAXSource
- Returns:
- the
XMLEventReader
- Throws:
IllegalArgumentException
- ifsource
isn't a JAXP 1.4StAXSource
or custom StAX Source
-
createStaxResult
Create a JAXP 1.4StAXResult
for the givenXMLStreamWriter
.- Parameters:
streamWriter
- the StAX stream writer- Returns:
- a result wrapping the
streamWriter
-
createStaxResult
Create a JAXP 1.4StAXResult
for the givenXMLEventWriter
.- Parameters:
eventWriter
- the StAX event writer- Returns:
- a result wrapping
streamReader
-
createCustomStaxResult
Create a custom, non-JAXP 1.4 StAXResult
for the givenXMLStreamWriter
.- Parameters:
streamWriter
- the StAX stream writer- Returns:
- a source wrapping the
streamWriter
-
createCustomStaxResult
Create a custom, non-JAXP 1.4 StAXResult
for the givenXMLEventWriter
.- Parameters:
eventWriter
- the StAX event writer- Returns:
- a source wrapping the
eventWriter
-
isStaxResult
Indicate whether the givenResult
is a JAXP 1.4 StAX Result or custom StAX Result.- Returns:
true
ifresult
is a JAXP 1.4StAXResult
or custom StAX Result;false
otherwise
-
getXMLStreamWriter
Return theXMLStreamWriter
for the given StAX Result.- Parameters:
result
- a JAXP 1.4StAXResult
- Returns:
- the
XMLStreamReader
- Throws:
IllegalArgumentException
- ifsource
isn't a JAXP 1.4StAXResult
or custom StAX Result
-
getXMLEventWriter
Return theXMLEventWriter
for the given StAX Result.- Parameters:
result
- a JAXP 1.4StAXResult
- Returns:
- the
XMLStreamReader
- Throws:
IllegalArgumentException
- ifsource
isn't a JAXP 1.4StAXResult
or custom StAX Result
-
createXMLEventReader
Create aXMLEventReader
from the given list ofXMLEvent
.- Parameters:
events
- the list ofXMLEvents
.- Returns:
- an
XMLEventReader
that reads from the given events - Since:
- 5.0
-
createContentHandler
Create a SAXContentHandler
that writes to the given StAXXMLStreamWriter
.- Parameters:
streamWriter
- the StAX stream writer- Returns:
- a content handler writing to the
streamWriter
-
createContentHandler
Create a SAXContentHandler
that writes events to the given StAXXMLEventWriter
.- Parameters:
eventWriter
- the StAX event writer- Returns:
- a content handler writing to the
eventWriter
-
createXMLReader
Create a SAXXMLReader
that reads from the given StAXXMLStreamReader
.- Parameters:
streamReader
- the StAX stream reader- Returns:
- a XMLReader reading from the
streamWriter
-
createXMLReader
Create a SAXXMLReader
that reads from the given StAXXMLEventReader
.- Parameters:
eventReader
- the StAX event reader- Returns:
- a XMLReader reading from the
eventWriter
-
createEventStreamReader
public static XMLStreamReader createEventStreamReader(XMLEventReader eventReader) throws XMLStreamException Return aXMLStreamReader
that reads from aXMLEventReader
. Useful because the StAXXMLInputFactory
allows one to create an event reader from a stream reader, but not vice-versa.- Returns:
- a stream reader that reads from an event reader
- Throws:
XMLStreamException
-
createEventStreamWriter
Return aXMLStreamWriter
that writes to aXMLEventWriter
.- Returns:
- a stream writer that writes to an event writer
- Since:
- 3.2
-
createEventStreamWriter
public static XMLStreamWriter createEventStreamWriter(XMLEventWriter eventWriter, XMLEventFactory eventFactory) Return aXMLStreamWriter
that writes to aXMLEventWriter
.- Returns:
- a stream writer that writes to an event writer
- Since:
- 3.0.5
-