parse xml with prefix - cSetHeader

Six Stars

parse xml with prefix - cSetHeader

I don't know how to parse this XML with Prefixes:

<?xml version=\"1.0\" ?>
<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">
	<S:Body>
		<ns2:uploadResponse xmlns:ns2=\"http://upload.service.ws.findxnet.com/\">
			<return>
				<code>0</code>
			</return>
		</ns2:uploadResponse>
	</S:Body>
</S:Envelope>

with cSetHeader Component.

I would need the code value of 0.


Accepted Solutions
Community Manager

Re: parse xml with prefix - cSetHeader

This can be a bit tricky in the ESB toolset. You are required to do more of the coding yourself here. But this is how you can achieve this.

 

First of all, your XML is not valid. I suspect that you have taken this from a Java String since you are escaping quotes (using backslashes). Your XML should look like this....

<?xml version="1.0" ?>
<S:Envelope
	xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
	<S:Body>
		<ns2:uploadResponse
			xmlns:ns2="http://upload.service.ws.findxnet.com/">
			<return>
				<code>0</code>
			</return>
		</ns2:uploadResponse>
	</S:Body>
</S:Envelope>

I'll show a dummy route that I configured to test this. The route looks like this...

Screen Shot 2019-01-15 at 12.12.11.png

I am reading from a file using a cFile, then reading the XML using the cSetHeader and then outputting the result using a tiny bit of code in the cProcessor component. 

 

The cSetHeader component is configured as below.....

Screen Shot 2019-01-15 at 12.12.42.png

I called the header "CodeHeader". I am using XPath as the language. The XPath is "/S:Envelope/S:Body/ns2:uploadResponse/return/code/text()".

 

In order to extract this we need to also configure the namespaces. This can be seen in the "Namespaces" table below.

In the cProcessor component, I have the following code....

System.out.println((String)exchange.getIn().getHeader("CodeHeader", String.class));

This simply prints the value of the "CodeHeader" header to the terminal. 


All Replies
Community Manager

Re: parse xml with prefix - cSetHeader

This can be a bit tricky in the ESB toolset. You are required to do more of the coding yourself here. But this is how you can achieve this.

 

First of all, your XML is not valid. I suspect that you have taken this from a Java String since you are escaping quotes (using backslashes). Your XML should look like this....

<?xml version="1.0" ?>
<S:Envelope
	xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
	<S:Body>
		<ns2:uploadResponse
			xmlns:ns2="http://upload.service.ws.findxnet.com/">
			<return>
				<code>0</code>
			</return>
		</ns2:uploadResponse>
	</S:Body>
</S:Envelope>

I'll show a dummy route that I configured to test this. The route looks like this...

Screen Shot 2019-01-15 at 12.12.11.png

I am reading from a file using a cFile, then reading the XML using the cSetHeader and then outputting the result using a tiny bit of code in the cProcessor component. 

 

The cSetHeader component is configured as below.....

Screen Shot 2019-01-15 at 12.12.42.png

I called the header "CodeHeader". I am using XPath as the language. The XPath is "/S:Envelope/S:Body/ns2:uploadResponse/return/code/text()".

 

In order to extract this we need to also configure the namespaces. This can be seen in the "Namespaces" table below.

In the cProcessor component, I have the following code....

System.out.println((String)exchange.getIn().getHeader("CodeHeader", String.class));

This simply prints the value of the "CodeHeader" header to the terminal. 

Six Stars

Re: parse xml with prefix - cSetHeader

Why is this text() method at the end?

Also I think we don't need a cast to String.

Community Manager

Re: parse xml with prefix - cSetHeader

The text() at the end removes the code tags surrounding the number. Otherwise you end up with <code>0</code>.

 

The reason for casting to a String was that I tried different code(without the String.class parameter as a parameter in the getHeader method) and simply left it in when I modified what I'd done. On looking at this, you are probably right that it is not needed. Just lazy testing on my part :-)

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Talend API Designer – Technical Overview

Take a look at this technical overview video of Talend API Designer

Watch Now

Getting Started with APIs

Find out how to get started with APIs

Read