You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "calyan.bandi" <ca...@gmail.com> on 2015/11/03 08:02:00 UTC

How to read XML Payload

Hi, 

Could someone please provide me the syntax for reading the XML payload with
only the relevant tags. My input XML contains data as below:

<PricePlans>
<Header>
 .......
</Header>
<Body>
 <customer>
 ....
 </customer>
 <customer>
 ....
 </customer>
 <customer>
 ....
 </customer>
</Body>
</PricePlans>

I need to split the input XML and do custom processing for each individual
customer. The XML after splitting should look as below i.e., from the entire
XML i should split only the 'customer' tags contained in the Body and the
non-body tags should remain untouched and added to the output XML. 

<PricePlans>
<Header>
 .......
</Header>
<Body>
 <customer>
 ....
 </customer>
</Body>
</PricePlans>


Any thoughts, please share.

Thanks,
Kalyan



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-read-XML-Payload-tp5773337.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to read XML Payload

Posted by "calyan.bandi" <ca...@gmail.com>.
Hi,

Thanks everyone for giving their suggestions. I was able to solve this using
xslt component in a camel-loop . I am giving the route here just for your
reference:

 <camel:route>
 	<from uri="file:in/xml?fileName=Customers.xml&amp;noop=true" />
 	<camel:loop copy="true">
		<camel:xpath
resultType="java.lang.Integer">count(/PricePlans/Body/customer)</camel:xpath>		
		<camel:setHeader headerName="loopIndex">
			<camel:property>CamelLoopIndex</camel:property>			
		</camel:setHeader>
		<camel:setHeader headerName="custIndex">
			<camel:simple>${header.loopIndex}++</camel:simple>
		</camel:setHeader>
				
		<camel:to uri="xslt:file:in/xslt/Customer.xsl" />
		<camel:to uri="direct:output" /> 		
 	</camel:loop>
 </camel:route>

In my XSLT, i am making use of the header "custIndex" to reach out to each
customer node. The XSLT looks as below. I was able to achieve this with the
feature provided in XSLT component which is "all headers are added as
parameters which are available in the XSLT". 

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="custIndex" />

			<xsl:template match="/">
				<customer>
					<header>
						<meta-data>
							<xsl:value-of select="PricePlans/Header/meta-data" />
						</meta-data>
						<date>
							<xsl:value-of select="PricePlans/Header/date" />
						</date>
					</header>
					<body>
						<customer>
							<id>
								<xsl:value-of select="PricePlans/Body/customer[$custIndex]/id" />
							</id>
							<name>
								<xsl:value-of select="PricePlans/Body/customer[$custIndex]/name" />
							</name>
						</customer>
					</body>
				</customer>
			</xsl:template>			
</xsl:stylesheet>


What bothers me here is the use of header values twice in the route. This is
because the camel's loop property 'CamelLoopIndex' starts with zero and i
cannot pass this directly into the XSLT file. The alternative way of doing
this via message translator looks good but i haven't tried it. Any ways, the
code is working now :)

Regards,
Kalyan



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-read-XML-Payload-tp5773337p5773431.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to read XML Payload

Posted by SopwithInOut <si...@sharklasers.com>.
I assume that by now, you will have already solved this. But in any case, I
would approach this with some kind of "/ Message Translator
<http://bit.ly/MTEIP>  /" solution in mind. 

I'm very, very new to Camel. But I have a couple years experience using a
big-name proprietary ESB. I've done similar transformations using that ESB's 
"/ Message Translator <http://bit.ly/MTEIP>  /" support. The low-level
details are different. But at a high-level...

I would start off with some kind of reference to a single empty
"/PricePlans/Body/" node (/call it $mtBody, for short/). Then for each
<customer> I come across, I'd squirt each one in turn into my
previously-prepared /$mtBody/ node. In pseudo-code...


		
Or assuming each of my /<customer>/ elements have some unique identifier —
maybe a "/custId/" attribute, say — it should be possible to forego a
looping approach. Instead, I could envisage some kind of  "/ Message
Translator <http://bit.ly/MTEIP>  /" that used XPath to somehow reference
each /<customer>/ by its /custID/ attribute.

How did you eventually solve it, by the way?





--
View this message in context: http://camel.465427.n5.nabble.com/How-to-read-XML-Payload-tp5773337p5773427.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: How to read XML Payload

Posted by "Vanshul.Chawla" <Va...@target.com>.
A way achieve this is with xslt.
Have the header part saved in Camel header and attach that to each split message using xslt.

-----Original Message-----
From: calyan.bandi [mailto:calyan.bandi@gmail.com] 
Sent: Wednesday, November 04, 2015 2:38 PM
To: users@camel.apache.org
Subject: Re: How to read XML Payload

Hi Henry,

I tried both the options XPath and tokenize. But they didn't give me the desired results. The route is defined as below:

<route>
<from uri="file:xml?noop=true" />
<split>
 <xpath>/PricePlans/Body/customer</xpath>
 <to uri="direct:processCustomer" />
</split>
</route>

The output that i receive from the XPath split is only the customer tag as shown below. 

<customer>
...
</customer>

I need the meta-data information in the original XML file to be passed on in the output XML. I even tried with the <tokenize /> as shown below but no luck.

<camel:tokenize token="customer" xml="true"
inheritNamespaceTagName="PricePlans" />

Thanks,
Kalyan



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-read-XML-Payload-tp5773337p5773368.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to read XML Payload

Posted by "calyan.bandi" <ca...@gmail.com>.
Hi Henry,

I tried both the options XPath and tokenize. But they didn't give me the
desired results. The route is defined as below:

<route>
<from uri="file:xml?noop=true" />
<split>
 <xpath>/PricePlans/Body/customer</xpath>
 <to uri="direct:processCustomer" />
</split>
</route>

The output that i receive from the XPath split is only the customer tag as
shown below. 

<customer>
...
</customer>

I need the meta-data information in the original XML file to be passed on in
the output XML. I even tried with the <tokenize /> as shown below but no
luck.

<camel:tokenize token="customer" xml="true"
inheritNamespaceTagName="PricePlans" />

Thanks,
Kalyan



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-read-XML-Payload-tp5773337p5773368.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to read XML Payload

Posted by Henryk Konsek <he...@gmail.com>.
Hi,

Have you taken a look at the XPath splitter by any chance?

Cheers!

wt., 3.11.2015 o 12:31 użytkownik calyan.bandi <ca...@gmail.com>
napisał:

> Hi,
>
> Could someone please provide me the syntax for reading the XML payload with
> only the relevant tags. My input XML contains data as below:
>
> <PricePlans>
> <Header>
>  .......
> </Header>
> <Body>
>  <customer>
>  ....
>  </customer>
>  <customer>
>  ....
>  </customer>
>  <customer>
>  ....
>  </customer>
> </Body>
> </PricePlans>
>
> I need to split the input XML and do custom processing for each individual
> customer. The XML after splitting should look as below i.e., from the
> entire
> XML i should split only the 'customer' tags contained in the Body and the
> non-body tags should remain untouched and added to the output XML.
>
> <PricePlans>
> <Header>
>  .......
> </Header>
> <Body>
>  <customer>
>  ....
>  </customer>
> </Body>
> </PricePlans>
>
>
> Any thoughts, please share.
>
> Thanks,
> Kalyan
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-read-XML-Payload-tp5773337.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
-- 
Henryk Konsek
http://about.me/hekonsek