Package org.springframework.util.xml

# Class StaxUtils

java.lang.Object
org.springframework.util.xml.StaxUtils

public abstract class StaxUtils extends Object
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

Constructors
Constructor
Description
`StaxUtils()`

## Method Summary

Modifier and Type
Method
Description
`static ContentHandler`
`createContentHandler(XMLEventWriter eventWriter)`
Create a SAX `ContentHandler` that writes events to the given StAX `XMLEventWriter`.
`static ContentHandler`
`createContentHandler(XMLStreamWriter streamWriter)`
Create a SAX `ContentHandler` that writes to the given StAX `XMLStreamWriter`.
`static Result`
`createCustomStaxResult(XMLEventWriter eventWriter)`
Create a custom, non-JAXP 1.4 StAX `Result` for the given `XMLEventWriter`.
`static Result`
`createCustomStaxResult(XMLStreamWriter streamWriter)`
Create a custom, non-JAXP 1.4 StAX `Result` for the given `XMLStreamWriter`.
`static Source`
`createCustomStaxSource(XMLEventReader eventReader)`
Create a custom, non-JAXP 1.4 StAX `Source` for the given `XMLEventReader`.
`static Source`
`createCustomStaxSource(XMLStreamReader streamReader)`
Create a custom, non-JAXP 1.4 StAX `Source` for the given `XMLStreamReader`.
`static XMLInputFactory`
`createDefensiveInputFactory()`
Create an `XMLInputFactory` with Spring's defensive setup, i.e.
`static <T extends XMLInputFactory>
T`
`createDefensiveInputFactory(Supplier<T> instanceSupplier)`
Variant of `createDefensiveInputFactory()` with a custom instance.
`static XMLStreamReader`
`createEventStreamReader(XMLEventReader eventReader)`
Return a `XMLStreamReader` that reads from a `XMLEventReader`.
`static XMLStreamWriter`
`createEventStreamWriter(XMLEventWriter eventWriter)`
Return a `XMLStreamWriter` that writes to a `XMLEventWriter`.
`static XMLStreamWriter`
`createEventStreamWriter(XMLEventWriter eventWriter, XMLEventFactory eventFactory)`
Return a `XMLStreamWriter` that writes to a `XMLEventWriter`.
`static Result`
`createStaxResult(XMLEventWriter eventWriter)`
Create a JAXP 1.4 `StAXResult` for the given `XMLEventWriter`.
`static Result`
`createStaxResult(XMLStreamWriter streamWriter)`
Create a JAXP 1.4 `StAXResult` for the given `XMLStreamWriter`.
`static Source`
`createStaxSource(XMLEventReader eventReader)`
Create a JAXP 1.4 `StAXSource` for the given `XMLEventReader`.
`static Source`
`createStaxSource(XMLStreamReader streamReader)`
Create a JAXP 1.4 `StAXSource` for the given `XMLStreamReader`.
`static XMLEventReader`
`createXMLEventReader(List<XMLEvent> events)`
Create a `XMLEventReader` from the given list of `XMLEvent`.
`static XMLReader`
`createXMLReader(XMLEventReader eventReader)`
Create a SAX `XMLReader` that reads from the given StAX `XMLEventReader`.
`static XMLReader`
`createXMLReader(XMLStreamReader streamReader)`
Create a SAX `XMLReader` that reads from the given StAX `XMLStreamReader`.
`static XMLEventReader`
`getXMLEventReader(Source source)`
Return the `XMLEventReader` for the given StAX Source.
`static XMLEventWriter`
`getXMLEventWriter(Result result)`
Return the `XMLEventWriter` for the given StAX Result.
`static XMLStreamReader`
`getXMLStreamReader(Source source)`
Return the `XMLStreamReader` for the given StAX Source.
`static XMLStreamWriter`
`getXMLStreamWriter(Result result)`
Return the `XMLStreamWriter` for the given StAX Result.
`static boolean`
`isStaxResult(Result result)`
Indicate whether the given `Result` is a JAXP 1.4 StAX Result or custom StAX Result.
`static boolean`
`isStaxSource(Source source)`
Indicate whether the given `Source` is a JAXP 1.4 StAX Source or custom StAX Source.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### StaxUtils

public StaxUtils()

## Method Details

### createDefensiveInputFactory

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:
  a new defensively initialized input factory instance to use
Since:
  5.0

### createDefensiveInputFactory

public static <T extends XMLInputFactory> T createDefensiveInputFactory(Supplier<T> instanceSupplier)
Variant of `createDefensiveInputFactory()` 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

public static Source createStaxSource(XMLStreamReader streamReader)
Create a JAXP 1.4 `StAXSource` for the given `XMLStreamReader`.

Parameters:
  `streamReader` - the StAX stream reader
Returns:
  a source wrapping the `streamReader`

### createStaxSource

public static Source createStaxSource(XMLEventReader eventReader) throws XMLStreamException
Create a JAXP 1.4 `StAXSource` for the given `XMLEventReader`.

Parameters:
  `eventReader` - the StAX event reader
Returns:
  a source wrapping the `eventReader`
Throws:
  `XMLStreamException`

### createCustomStaxSource

public static Source createCustomStaxSource(XMLStreamReader streamReader)
Create a custom, non-JAXP 1.4 StAX `Source` for the given `XMLStreamReader`.

Parameters:
  `streamReader` - the StAX stream reader
Returns:
  a source wrapping the `streamReader`

### createCustomStaxSource

public static Source createCustomStaxSource(XMLEventReader eventReader)
Create a custom, non-JAXP 1.4 StAX `Source` for the given `XMLEventReader`.

Parameters:
  `eventReader` - the StAX event reader
Returns:
  a source wrapping the `eventReader`

### isStaxSource

public static boolean isStaxSource(Source source)
Indicate whether the given `Source` is a JAXP 1.4 StAX Source or custom StAX Source.

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

### getXMLStreamReader

@Nullable public static XMLStreamReader getXMLStreamReader(Source source)
Return the `XMLStreamReader` for the given StAX Source.

Parameters:
  `source` - a JAXP 1.4 `StAXSource`
Returns:
  the `XMLStreamReader`
Throws:
  `IllegalArgumentException` - if `source` isn't a JAXP 1.4 `StAXSource` or custom StAX Source

### getXMLEventReader

@Nullable public static XMLEventReader getXMLEventReader(Source source)
Return the `XMLEventReader` for the given StAX Source.

Parameters:
  `source` - a JAXP 1.4 `StAXSource`
Returns:
  the `XMLEventReader`
Throws:
  `IllegalArgumentException` - if `source` isn't a JAXP 1.4 `StAXSource` or custom StAX Source

### createStaxResult

public static Result createStaxResult(XMLStreamWriter streamWriter)
Create a JAXP 1.4 `StAXResult` for the given `XMLStreamWriter`.

Parameters:
  `streamWriter` - the StAX stream writer
Returns:
  a result wrapping the `streamWriter`

### createStaxResult

public static Result createStaxResult(XMLEventWriter eventWriter)
Create a JAXP 1.4 `StAXResult` for the given `XMLEventWriter`.

Parameters:
  `eventWriter` - the StAX event writer
Returns:
  a result wrapping `streamReader`

### createCustomStaxResult

public static Result createCustomStaxResult(XMLStreamWriter streamWriter)
Create a custom, non-JAXP 1.4 StAX `Result` for the given `XMLStreamWriter`.

Parameters:
  `streamWriter` - the StAX stream writer
Returns:
  a source wrapping the `streamWriter`

### createCustomStaxResult

public static Result createCustomStaxResult(XMLEventWriter eventWriter)
Create a custom, non-JAXP 1.4 StAX `Result` for the given `XMLEventWriter`.

Parameters:
  `eventWriter` - the StAX event writer
Returns:
  a source wrapping the `eventWriter`

### isStaxResult

public static boolean isStaxResult(Result result)
Indicate whether the given `Result` is a JAXP 1.4 StAX Result or custom StAX Result.

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

### getXMLStreamWriter

@Nullable public static XMLStreamWriter getXMLStreamWriter(Result result)
Return the `XMLStreamWriter` for the given StAX Result.

Parameters:
  `result` - a JAXP 1.4 `StAXResult`
Returns:
  the `XMLStreamReader`
Throws:
  `IllegalArgumentException` - if `source` isn't a JAXP 1.4 `StAXResult` or custom StAX Result

### getXMLEventWriter

@Nullable public static XMLEventWriter getXMLEventWriter(Result result)
Return the `XMLEventWriter` for the given StAX Result.

Parameters:
  `result` - a JAXP 1.4 `StAXResult`
Returns:
  the `XMLStreamReader`
Throws:
  `IllegalArgumentException` - if `source` isn't a JAXP 1.4 `StAXResult` or custom StAX Result

### createXMLEventReader

public static XMLEventReader createXMLEventReader(List<XMLEvent> events)
Create a `XMLEventReader` from the given list of `XMLEvent`.

Parameters:
  `events` - the list of `XMLEvents`.
Returns:
  an `XMLEventReader` that reads from the given events
Since:
  5.0

### createContentHandler

public static ContentHandler createContentHandler(XMLStreamWriter streamWriter)
Create a SAX `ContentHandler` that writes to the given StAX `XMLStreamWriter`.

Parameters:
  `streamWriter` - the StAX stream writer
Returns:
  a content handler writing to the `streamWriter`

### createContentHandler

public static ContentHandler createContentHandler(XMLEventWriter eventWriter)
Create a SAX `ContentHandler` that writes events to the given StAX `XMLEventWriter`.

Parameters:
  `eventWriter` - the StAX event writer
Returns:
  a content handler writing to the `eventWriter`

### createXMLReader

public static XMLReader createXMLReader(XMLStreamReader streamReader)
Create a SAX `XMLReader` that reads from the given StAX `XMLStreamReader`.

Parameters:
  `streamReader` - the StAX stream reader
Returns:
  a XMLReader reading from the `streamWriter`

### createXMLReader

public static XMLReader createXMLReader(XMLEventReader eventReader)
Create a SAX `XMLReader` that reads from the given StAX `XMLEventReader`.

Parameters:
  `eventReader` - the StAX event reader
Returns:
  a XMLReader reading from the `eventWriter`

### createEventStreamReader

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:
  a stream reader that reads from an event reader
Throws:
  `XMLStreamException`

### createEventStreamWriter

public static XMLStreamWriter createEventStreamWriter(XMLEventWriter eventWriter)
Return a `XMLStreamWriter` that writes to a `XMLEventWriter`.

Returns:
  a stream writer that writes to an event writer
Since:
  3.2

### createEventStreamWriter

public static XMLStreamWriter createEventStreamWriter(XMLEventWriter eventWriter, XMLEventFactory eventFactory)
Return a `XMLStreamWriter` that writes to a `XMLEventWriter`.

Returns:
  a stream writer that writes to an event writer
Since:
  3.0.5