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; 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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ContentHandlercreateContentHandler(xmlEventWriter eventWriter) Create a SAXContentHandlerthat writes events to the given StAXxmlEventWriter.static ContentHandlercreateContentHandler(xmlStreamWriter streamWriter) Create a SAXContentHandlerthat writes to the given StAXxmlStreamWriter.static ResultcreateCustomStaxResult(xmlEventWriter eventWriter) Create a custom, non-JAXP 1.4 StAXResultfor the givenxmlEventWriter.static ResultcreateCustomStaxResult(xmlStreamWriter streamWriter) Create a custom, non-JAXP 1.4 StAXResultfor the givenxmlStreamWriter.static SourcecreateCustomStaxSource(xmlEventReader eventReader) Create a custom, non-JAXP 1.4 StAXSourcefor the givenxmlEventReader.static SourcecreateCustomStaxSource(xmlStreamReader streamReader) Create a custom, non-JAXP 1.4 StAXSourcefor the givenxmlStreamReader.static xmlInputFactoryCreate anxmlInputFactorywith Spring's defensive setup, i.e.static <T extends xmlInputFactory>
TcreateDefensiveInputFactory(Supplier<T> instanceSupplier) Variant ofcreateDefensiveInputFactory()with a custom instance.static xmlStreamReadercreateEventStreamReader(xmlEventReader eventReader) Return axmlStreamReaderthat reads from axmlEventReader.static xmlStreamWritercreateEventStreamWriter(xmlEventWriter eventWriter) Return axmlStreamWriterthat writes to axmlEventWriter.static xmlStreamWritercreateEventStreamWriter(xmlEventWriter eventWriter, xmlEventFactory eventFactory) Return axmlStreamWriterthat writes to axmlEventWriter.static ResultcreateStaxResult(xmlEventWriter eventWriter) Create a JAXP 1.4StAXResultfor the givenxmlEventWriter.static ResultcreateStaxResult(xmlStreamWriter streamWriter) Create a JAXP 1.4StAXResultfor the givenxmlStreamWriter.static SourcecreateStaxSource(xmlEventReader eventReader) Create a JAXP 1.4StAXSourcefor the givenxmlEventReader.static SourcecreateStaxSource(xmlStreamReader streamReader) Create a JAXP 1.4StAXSourcefor the givenxmlStreamReader.static xmlEventReadercreatexmlEventReader(List<xmlEvent> events) Create axmlEventReaderfrom the given list ofxmlEvent.static xmlReadercreatexmlReader(xmlEventReader eventReader) Create a SAXxmlReaderthat reads from the given StAXxmlEventReader.static xmlReadercreatexmlReader(xmlStreamReader streamReader) Create a SAXxmlReaderthat reads from the given StAXxmlStreamReader.static @Nullable xmlEventReadergetxmlEventReader(Source source) Return thexmlEventReaderfor the given StAX Source.static @Nullable xmlEventWritergetxmlEventWriter(Result result) Return thexmlEventWriterfor the given StAX Result.static @Nullable xmlStreamReadergetxmlStreamReader(Source source) Return thexmlStreamReaderfor the given StAX Source.static @Nullable xmlStreamWritergetxmlStreamWriter(Result result) Return thexmlStreamWriterfor the given StAX Result.static booleanisStaxResult(Result result) Indicate whether the givenResultis a JAXP 1.4 StAX Result or custom StAX Result.static booleanisStaxSource(Source source) Indicate whether the givenSourceis a JAXP 1.4 StAX Source or custom StAX Source.
-
Constructor Details
-
StaxUtils
public StaxUtils()
-
-
Method Details
-
createDefensiveInputFactory
Create anxmlInputFactorywith 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.4StAXSourcefor the givenxmlStreamReader.- Parameters:
streamReader- the StAX stream reader- Returns:
- a source wrapping the
streamReader
-
createStaxSource
Create a JAXP 1.4StAXSourcefor the givenxmlEventReader.- Parameters:
eventReader- the StAX event reader- Returns:
- a source wrapping the
eventReader - Throws:
xmlStreamException
-
createCustomStaxSource
Create a custom, non-JAXP 1.4 StAXSourcefor the givenxmlStreamReader.- Parameters:
streamReader- the StAX stream reader- Returns:
- a source wrapping the
streamReader
-
createCustomStaxSource
Create a custom, non-JAXP 1.4 StAXSourcefor the givenxmlEventReader.- Parameters:
eventReader- the StAX event reader- Returns:
- a source wrapping the
eventReader
-
isStaxSource
Indicate whether the givenSourceis a JAXP 1.4 StAX Source or custom StAX Source.- Returns:
trueifsourceis a JAXP 1.4StAXSourceor custom StAX Source;falseotherwise
-
getxmlStreamReader
Return thexmlStreamReaderfor the given StAX Source.- Parameters:
source- a JAXP 1.4StAXSource- Returns:
- the
xmlStreamReader - Throws:
IllegalArgumentException- ifsourceisn't a JAXP 1.4StAXSourceor custom StAX Source
-
getxmlEventReader
Return thexmlEventReaderfor the given StAX Source.- Parameters:
source- a JAXP 1.4StAXSource- Returns:
- the
xmlEventReader - Throws:
IllegalArgumentException- ifsourceisn't a JAXP 1.4StAXSourceor custom StAX Source
-
createStaxResult
Create a JAXP 1.4StAXResultfor the givenxmlStreamWriter.- Parameters:
streamWriter- the StAX stream writer- Returns:
- a result wrapping the
streamWriter
-
createStaxResult
Create a JAXP 1.4StAXResultfor the givenxmlEventWriter.- Parameters:
eventWriter- the StAX event writer- Returns:
- a result wrapping
streamReader
-
createCustomStaxResult
Create a custom, non-JAXP 1.4 StAXResultfor the givenxmlStreamWriter.- Parameters:
streamWriter- the StAX stream writer- Returns:
- a source wrapping the
streamWriter
-
createCustomStaxResult
Create a custom, non-JAXP 1.4 StAXResultfor the givenxmlEventWriter.- Parameters:
eventWriter- the StAX event writer- Returns:
- a source wrapping the
eventWriter
-
isStaxResult
Indicate whether the givenResultis a JAXP 1.4 StAX Result or custom StAX Result.- Returns:
trueifresultis a JAXP 1.4StAXResultor custom StAX Result;falseotherwise
-
getxmlStreamWriter
Return thexmlStreamWriterfor the given StAX Result.- Parameters:
result- a JAXP 1.4StAXResult- Returns:
- the
xmlStreamReader - Throws:
IllegalArgumentException- ifsourceisn't a JAXP 1.4StAXResultor custom StAX Result
-
getxmlEventWriter
Return thexmlEventWriterfor the given StAX Result.- Parameters:
result- a JAXP 1.4StAXResult- Returns:
- the
xmlStreamReader - Throws:
IllegalArgumentException- ifsourceisn't a JAXP 1.4StAXResultor custom StAX Result
-
createxmlEventReader
Create axmlEventReaderfrom the given list ofxmlEvent.- Parameters:
events- the list ofxmlEvents.- Returns:
- an
xmlEventReaderthat reads from the given events - Since:
- 5.0
-
createContentHandler
Create a SAXContentHandlerthat writes to the given StAXxmlStreamWriter.- Parameters:
streamWriter- the StAX stream writer- Returns:
- a content handler writing to the
streamWriter
-
createContentHandler
Create a SAXContentHandlerthat writes events to the given StAXxmlEventWriter.- Parameters:
eventWriter- the StAX event writer- Returns:
- a content handler writing to the
eventWriter
-
createxmlReader
Create a SAXxmlReaderthat reads from the given StAXxmlStreamReader.- Parameters:
streamReader- the StAX stream reader- Returns:
- a xmlReader reading from the
streamWriter
-
createxmlReader
Create a SAXxmlReaderthat 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 axmlStreamReaderthat reads from axmlEventReader. Useful because the StAXxmlInputFactoryallows 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 axmlStreamWriterthat 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 axmlStreamWriterthat writes to axmlEventWriter.- Returns:
- a stream writer that writes to an event writer
- Since:
- 3.0.5
-