이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 122. StAX


StAX Component

Available as of Camel 2.9
The StAX component allows messages to be process through a SAX ContentHandler. Another 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:
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-stax</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>
Copy to Clipboard Toggle word wrap

URI format

stax:content-handler-class
Copy to Clipboard Toggle word wrap
example:
stax:org.superbiz.FooContentHandler
Copy to Clipboard Toggle word wrap
From Camel 2.11.1 onwards you can lookup a org.xml.sax.ContentHandler bean from the Registry using the # syntax as shown:
stax:#myHandler
Copy to Clipboard Toggle word wrap

Usage of a content handler as StAX parser

The message body after the handling is the handler itself.
Here an example:
from("file:target/in")
  .to("stax: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
    }
  });
Copy to Clipboard Toggle word wrap

Iterate over a collection using JAXB and StAX

First we suppose you have JAXB objects.
For instance a list of records in a wrapper object:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "records")
public class Records {
    @XmlElement(required = true)
    protected List<Record> record;

    public List<Record> getRecord() {
        if (record == null) {
            record = new ArrayList<Record>();
        }
        return record;
    }
}
Copy to Clipboard Toggle word wrap
and
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "record", propOrder = { "key", "value" })
public class Record {
    @XmlAttribute(required = true)
    protected String key;

    @XmlAttribute(required = true)
    protected String value;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
Copy to Clipboard Toggle word wrap
Then you get a XML file to process:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<records>
  <record value="v0" key="0"/>
  <record value="v1" key="1"/>
  <record value="v2" key="2"/>
  <record value="v3" key="3"/>
  <record value="v4" key="4"/>
  <record value="v5" key="5"/>
</record>
Copy to Clipboard Toggle word wrap
The StAX component provides an StAXBuilder which can be used when iterating XML elements with the Camel Splitter
from("file:target/in")
    .split(stax(Record.class)).streaming()
        .to("mock:records");
Copy to Clipboard Toggle word wrap
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:
from("file:target/in")
    .split(stax(Record.class, false)).streaming()
        .to("mock:records");
Copy to Clipboard Toggle word wrap

The previous example with XML DSL

The example above could be implemented as follows in XML DSL
<!-- use STaXBuilder to create the expression we want to use in the route below for splitting the XML file -->
<!-- notice we use the factory-method to define the stax method, and to pass in the parameter as a constructor-arg -->
<bean id="staxRecord" class="org.apache.camel.component.stax.StAXBuilder" factory-method="stax">
  <!-- FQN class name of the POJO with the JAXB annotations -->
  <constructor-arg index="0" value="org.apache.camel.component.stax.model.Record"/>
</bean>

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <!-- pickup XML files -->
    <from uri="file:target/in"/>
    <split streaming="true">
      <!-- split the file using StAX (ref to bean above) -->
      <!-- and use streaming mode in the splitter -->
      <ref>staxRecord</ref>
      <!-- and send each splitted to a mock endpoint, which will be a Record POJO instance -->
      <to uri="mock:records"/>
    </split>
  </route>
</camelContext>
Copy to Clipboard Toggle word wrap
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat