Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Toronto Sept 9th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

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.

Labels (4)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

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...

0683p000009M1x2.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.....

0683p000009M1x7.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. 

View solution in original post

3 Replies
Anonymous
Not applicable
Author

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...

0683p000009M1x2.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.....

0683p000009M1x7.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. 

Anonymous
Not applicable
Author

Why is this text() method at the end?

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

Anonymous
Not applicable
Author

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 🙂