Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

StAX Component

Available as of Camel 2.9

The StAX component allows messages to be process through a SAX ContentHandler.
The implementation convert a SAX ContentHanler to a XMLStreamReader.
Another great feature of this component is to allow to iterate over JAXB records using StAX, for example using the Splitter EIP.

Maven users will need to add the following dependency to their pom.xml for this component:

...

Code Block
stax:org.superbiz.FooContentHandler

Converter

Some converters are provided by the component.

It allows you to convert a GenericFile or a File to a SAXSource, and a GenericFile or a String (representing a file path) to either a XMLStreamReader or a XMLEventReader.

Usage of a content handler as StAX parser

...

Code Block
java
java
from("file:target/in")
  .to("StAXstax:org.superbiz.handler.CountingHandler") 
  // CountingHandler implements org.xml.sax.ContentHandler or extends org.xml.sax.helpers.DefaultHandler
  .process(new Processor() {
    @Override
    public void process(Exchange exchange) throws Exception {
        CountingHandler handler = exchange.getIn().getBody(CountingHandler.class);
        // do some great work with the handler
    }
  });

...

The StAX component provides an iterator call org.apache.camel.stax.StAXJAXBIteratorExpression which allows you
to iterate through this collection with the camel splitter: StAXBuilder which can be used when iterating XML elements with the Camel Splitter

Code Block
java
java
from("file:target/in")
  .setBody(header(Exchange.FILE_PATH))
      .split(new StAXJAXBIteratorExpression<Record>stax(Record.class)).streaming()
          .to("mock:records");

The body will be the constructor parameter class (Record in the snippet).

...

Where stax is a static method on org.apache.camel.component.stax.StAXBuilder which you can static import in the Java code.

The previous example with XML DSL

The example above could be implemented as follows in XML DSL

Wiki Markup
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-stax/src/test/resources/org/apache/camel/component/stax/SpringStAXJAXBIteratorExpressionTest.xml}
Include Page
CAMEL:Endpoint See Also
CAMEL:Endpoint See Also