Nux 1.6

nux.xom.io
Class StaxUtil

java.lang.Object
  extended by nux.xom.io.StaxUtil

public class StaxUtil
extends Object

Various utilities; a XOM Builder implementation that uses a StAX parser instead of a SAX parser; a XMLStreamReader implementation reading from an underlying XOM Document or fragment; plus other tools.

Also see AXIOM StAX introduction.

Author:
whoschek.AT.lbl.DOT.gov, $Author: hoschek $

Method Summary
static Builder createBuilder(XMLInputFactory inputFactory, NodeFactory factory)
          Constructs and returns a Builder implementation that uses a StAX parser instead of a SAX parser.
static XMLStreamReader createXMLStreamReader(InputStream input, String baseURI)
          Constructs and returns a StAX XML pull parser for the given input stream.
static XMLStreamReader createXMLStreamReader(Node root)
          Constructs and returns a StAX XMLStreamReader pull parser implementation that reads from an underlying XOM Node; typically a Document or fragment (subtree); Ideal for efficient conversion of a XOM tree to SOAP/AXIOM, JAXB 2, JiBX or XMLBeans, for example when incrementally converting XQuery results via an Unmarshaller, perhaps in combination with a StreamingPathFilter.
static String toString(int eventType)
          Returns a debug string representation for the given StAX event type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createXMLStreamReader

public static XMLStreamReader createXMLStreamReader(Node root)
Constructs and returns a StAX XMLStreamReader pull parser implementation that reads from an underlying XOM Node; typically a Document or fragment (subtree); Ideal for efficient conversion of a XOM tree to SOAP/AXIOM, JAXB 2, JiBX or XMLBeans, for example when incrementally converting XQuery results via an Unmarshaller, perhaps in combination with a StreamingPathFilter.

The parser is namespace aware, non-validating and text coalescing.

Example usage:

  Document doc = new Builder().build("samples/data/articles.xml");
  Nodes results = XQueryUtil.xquery(doc, "/articles/article");
  Unmarshaller unmarshaller = JAXBContext.newInstance(...).createUnmarshaller();
  
  for (int i=0; i < results.size(); i++) {
      XMLStreamReader reader = StaxUtil.createXMLStreamReader(results.get(i));
      Object jaxbObject = unmarshaller.unmarshall(reader);
      ... do something with the JAXB object
  }
 

Parameters:
root - the root node of the subtree to read from; typically a Document or Element; can be parentless. XMLStreamConstants.START_DOCUMENT and XMLStreamConstants.END_DOCUMENT events will not be emitted if the root is an Element, i.e. a fragment. If the root is not a ParentNode, the XMLStreamReader's method hasNext() will always return false.
Returns:
a StAX pull parser reading from an underlying XOM Node

createBuilder

public static Builder createBuilder(XMLInputFactory inputFactory,
                                    NodeFactory factory)
Constructs and returns a Builder implementation that uses a StAX parser instead of a SAX parser. Can be used for polymorphic pluggability of SAX vs. StAX.

Parameters:
inputFactory - a factory constructing StAX XMLStreamReader instances. May be null in which case a default factory is used, producing a parser that will be namespace-aware, DTD-aware, non-validating, and in text-coalescing mode. In this case the preferred implementation is Woodstox, if available.
factory - the node factory to stream into. May be null in which case the default XOM NodeFactory is used, building the full XML tree.
Returns:
a Builder implementation using StAX instead of SAX

createXMLStreamReader

public static XMLStreamReader createXMLStreamReader(InputStream input,
                                                    String baseURI)
                                             throws ParsingException
Constructs and returns a StAX XML pull parser for the given input stream.

The StAX parser will be namespace-aware, DTD-aware, non-validating, and in text-coalescing mode.

Parameters:
input - the input stream to read from (must not be null)
baseURI - the base URI for the input's document (may be null or empty)
Returns:
a StAX pull parser
Throws:
ParsingException - if there is an error processing the underlying XML source

toString

public static String toString(int eventType)
Returns a debug string representation for the given StAX event type.

Parameters:
eventType - the StAX event type, e.g. XMLStreamConstants.START_ELEMENT.
Returns:
a debug string

Nux 1.6