Lets see how to extract data from XML files using Xpath in camel.
Create a java project in eclipse.
Create a folder by name testFileFolder and place the below given file.
The xml file that we will be using it, lets call it demo.xml
<emp>
<name>admin</name>
<phone>1234567890</phone>
<address>address 1</address>
<address>address 2</address>
</emp>
We will be extracting the address data alone from the xml.
Next create a java file by name MyService.java as given below,
package com.techexample.processor;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class MyService implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
String data=(String)exchange.getIn().getBody();
System.out.println(data);
}
}
Now the main part, we are going to create the camel context file.
Create a file with name camelContext.xml and place it inside a folder say Resource
The file context is,
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="splitdata" class="com.techexample.processor.MyService" />
<camel:camelContext id="readfile" trace="true">
<camel:route id="CsvSplit" trace="true">
<camel:from uri="file:testFileFolder" />
<camel:split parallelProcessing="false">
<camel:xpath>/emp/address/text()</camel:xpath>
<camel:convertBodyTo type="java.lang.String"/>
<camel:to uri="bean:splitdata" />
</camel:split>
</camel:route>
</camel:camelContext>
</beans>
Now, we have to write a java file to bring up the camel context file.
Click this link for the java file,
MyClient.java
Steps to run the camel project
Go to project properties, under java buildpath -> source , add the resource and testfileFolder.
Now right click in our MyClient.java file and click runAs -> java application
OUTPUT:
address 1
address 2