How to split and search through an XML file with Camel

XML is one of the most used payloads in enterprise integrations. In this tutorial we will show how you can use Camel’s support for splitting an XML file using XPath support and process fragments individually.

Introduction

Apache Camel is an open-source integration framework that allows you to connect various systems and software applications. One of the features provided by Camel is the ability to split a message into multiple pieces and process them individually. This can be useful when you need to process a large message or when you want to send a message to multiple recipients.

In this tutorial, we will go through an example of splitting an XML document with Camel.

Setting up the project

To split an XML document with Camel, you will need to add the following dependency to your project’s POM file:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-xpath</artifactId>
    <version>x.x.x</version>
</dependency>

Splitting an XML document

To split an XML document, you can use the split method and specify an XPath expression to identify the nodes to split on. For example, consider the following XML document:

<?xml version="1.0" encoding="UTF-8"?><catalog>
       
   <book id="id100">
             
      <author age="44">John Smith</author>
             
      <title>Java Developer's Guide</title>
             
      <genre>Computer</genre>
             
      <price>11.95</price>
          
   </book>
       
   <book id="id101">
             
      <author age="24">Sally Joy</author>
             
      <title>A tale</title>
             
      <genre>Fantasy</genre>
             
      <price>1.95</price>
          
   </book>
    
</catalog>

Next, we will code a Route to collect information such as the author name:

package com.mastertheboss.camel;
import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder extends RouteBuilder {
  public void configure() {
    from("file:src/data?noop=true").split(xpath("//catalog/book/author/text()")).to("log:output");
  }
}

The above Camel route assumes that the source XML documents are in the data folder. As a result, it will print the author names according to the XPath query: //catalog/book/author/text()

Output:

John Smith Sally Joy

Let’s see a more complex XPath expression:

package com.mastertheboss.camel;

import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder2 extends RouteBuilder {
  public void configure() {
    from("file:src/data?noop=true")
        .split(xpath("//catalog/book[@id='id100']/author/text()"))
        .to("log:output");
  }
}

In the above example, we will restrict the search to authors whose id matches with ‘id100’

Output:

John Smith

You can also apply Math expressions on the elements in the XML. Let’s pickup just authors over 40:

package com.mastertheboss.camel;

import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder3 extends RouteBuilder {
  public void configure() {
    from("file:src/data?noop=true")
        .split(xpath("//catalog/book/author[@age<40]/text()"))
        .to("log:output");
  }
}

Output:

Sally Joy

Processing the split messages

After the XML document has been split, you can process the individual messages in the route. For example:

from("direct:start")
    .split().xpath("/items/item")
    .process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            // Process the split message
            // ...
        }
    })
    .to("mock:end");

In this example, you can use the process method for each split message and you can perform any processing you need.

Source code: https://github.com/fmarchioni/masterspringboot/tree/master/camel/camel-split-xml

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon