Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Camel - Getting xpath value from XML

I'm trying to make a program by using camel. This program should be able to recieve xml files in a folder called inbox_xml_files.

Then the program should get the text value of the < Country > element node inside the xml file.

If the text value is "Denmark" the file should be moved to a folder called "outbox_Denmark".

If the text value is "Sweden" the file should be moved to a folder called "outbox_Sweden".

If the text value is something else the file should be moved to a folder called "outbox_Other".

This is the XML file i use for testing:

    <?xml version="1.0"?>
<Company>
  <Employee>
      <FirstName>Mike</FirstName>
      <LastName>James</LastName>
      <ContactNo>1234567890</ContactNo>
      <Email>[email protected]</Email>
      <Address>
            <Country>Denmark</Country>
            <City>Copenhagen</City>
            <Zip>1234</Zip>
      </Address>
  </Employee>
</Company>

The camel xml file (The one I'm troubled with):

<camelContext id="camelId" xmlns="http://camel.apache.org/schema/spring">
        <camel:route id="_route1">
            <camel:from id="_from1" uri="file:C:/inbox_xml_files?noop=false"/>
            <camel:choice id="_choice1">
                <camel:when id="_when1">
                    <camel:xpath>
                            //Company/Employee/Address/Country = 'Denmark'
                        </camel:xpath>
                    <camel:to id="_to1" uri="file:C:/outbox_Denmark"/>
                </camel:when>
                <camel:when id="_when2">
                    <camel:xpath>
                            //Company/Employee/Address/Country = 'Sweden'
                        </camel:xpath>
                    <camel:to id="_to2" uri="file:C:/outbox_Sweden"/>
                </camel:when>
                <camel:otherwise id="_otherwise1">
                    <camel:to id="_to3" uri="file:C:/outbox_Other"/>
                </camel:otherwise>
            </camel:choice>
        </camel:route>
    </camelContext>

I believe that the problem is inside the < camel:xpath >. I think i have defined it wrong and i'm trying but all this is something new for me so i'm struggling with it.

like image 736
Yoseph Avatar asked May 18 '26 14:05

Yoseph


1 Answers

Try this :

<camel:xpath>//Company/Employee/Address/Country='Sweden'</camel:xpath>

or

<camel:xpath>//*[local-name() = 'Country' and text()='Sweden']</camel:xpath>
like image 168
Ravi Joshi Avatar answered May 21 '26 02:05

Ravi Joshi