Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

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.

...

From Camel 2.11.1 onwards you can lookup a org.xml.sax.ContentHandler bean from the Registry using the # syntax as shown:

Code Block

stax:#myHandler

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")
    .setBodysplit(headerstax(Exchange.FILE_PATH)Record.class)).streaming()
        .split(new StAXJAXBIteratorExpression<Record>to("mock:records");

Where stax is a static method on org.apache.camel.component.stax.StAXBuilder which you can static import in the Java code. The stax builder is by default namespace aware on the XMLReader it uses. From Camel 2.11.1 onwards you can turn this off by setting the boolean parameter to false, as shown below:

Code Block
java
java

from("file:target/in")
    .split(stax(Record.class, false)).streaming()
          .to("mock:records");

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
Endpoint See Also
Endpoint See Also