Author |
Message
|
phaniIIB |
Posted: Sun Aug 23, 2015 6:48 am Post subject: Unable to traverse through XML message tree using XPath |
|
|
Acolyte
Joined: 28 Jul 2015 Posts: 51
|
I am trying to traverse through the following XML message using XPath,
Code: |
(0x01000000:Folder):XMLNSC = ( ['xmlnsc' : 0x7f7b1451b760]
(0x01000000:Folder)http://websvcs.otswebws:dequeue2Response = ( ['xmlnsc' : 0x7f7b146227a0]
(0x03000102:NamespaceDecl)http://www.w3.org/2000/xmlns/:p917 = 'http://websvcs.otswebws' (CHARACTER)
(0x01000000:Folder ):dequeue2Return = (
(0x03000000:PCDataField ):count = 570 (INTEGER)
|
My java code is:
Code: |
MbNamespaceBindings ns = new MbNamespaceBindings();
ns.addBinding("otns", "http://websvcs.otswebws");
ns.setDefaultNamespace("http://websvcs.otswebws");
MbXPath xp = new MbXPath("string(XMLNSC/otns:dequeue2Response/dequeue2Return/count)");
System.out.println("++++++++++++++++++++"+xp);
String msgCount = (String)outMessage.evaluateXPath(xp);
System.out.println("++++++++++++++++++++"+msgCount);
|
It doesn't print anything. Please verify my code and let me know what's wrong with the code. |
|
Back to top |
|
 |
lfrestrepog |
Posted: Mon Aug 24, 2015 12:20 am Post subject: |
|
|
Novice
Joined: 08 Jul 2014 Posts: 22
|
Hello.
Assuming your XML document looks like this:
Code: |
<tns:dequeue2Response xmlns:tns="http://websvcs.otswebws">
<dequeue2Return>
<count>570</count>
</dequeue2Return>
</tns:dequeue2Response>
|
1. Notice that the default namespace in that document is empty, you should not set it to "http://websvcs.otswebws"
2. You are not actually using that ns object, it can be set in the constructor of the XPath or using its "setNamespaceBindings" method
3. I think you don't need to specify the "XMLNSC" element in the XPath expression...
Also, printing the xp object is probably not too useful, although it should work.
Regards. _________________ --
Luis Fernando Restrepo Gutiérrez |
|
Back to top |
|
 |
phaniIIB |
Posted: Mon Aug 24, 2015 5:55 am Post subject: |
|
|
Acolyte
Joined: 28 Jul 2015 Posts: 51
|
I removed the default namespace and XMLNSC from the path, it throws the below exception,
'Namespace prefix not found in iNamespacePrefixes'
Code: |
MbNamespaceBindings ns = new MbNamespaceBindings();
ns.addBinding("otns", "http://websvcs.otswebws");
//ns.setDefaultNamespace("http://websvcs.otswebws");
MbXPath xp = new MbXPath("string(/otns:dequeue2Response/dequeue2Return/count)");
System.out.println("++++++++++++++++++++"+xp);
String msgCount = (String)outMessage.evaluateXPath(xp);
//List msgCount = (List)outMessage.evaluateXPath(xp);
System.out.println("++++++++++++++++++++"+msgCount);
|
Please Suggest!! |
|
Back to top |
|
 |
mqjeff |
Posted: Mon Aug 24, 2015 7:31 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
You didn't follow step 2.
You also *are* actually that namespace - at *least* on the top element, if not on the rest of the elements.
So you will need to bind it into your Xpath object and you will need to include it in your Xpath expression.
For a much better idea of how you should build your Xpath expression, you should use a Trace node to output the ${Root} message tree. This will show you all of the properties of every node under your XMLNSC data. _________________ chmod -R ugo-wx / |
|
Back to top |
|
 |
phaniIIB |
Posted: Mon Aug 24, 2015 10:06 am Post subject: |
|
|
Acolyte
Joined: 28 Jul 2015 Posts: 51
|
I did not understand. I am a newbie to this. Can you please explain it in an easy and granular manner.
Thank you in advance, |
|
Back to top |
|
 |
mqjeff |
Posted: Mon Aug 24, 2015 10:49 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
lfrestrepog wrote: |
2. You are not actually using that ns object, it can be set in the constructor of the XPath or using its "setNamespaceBindings" method |
That's step 2. You didn't do it, or at least you didn't show that you did.
Again, you need to use a Trace node. Insert it into your flow before your JavaCompute node.
Set the Pattern to ${Root}. Decide where you want the data to be output.
Run your flow.
Look at the output from the Trace node. Determine where you need to add namespace qualifiers to the parts of your Xpath.
Please review the documentation in the KnowledgeCenter. _________________ chmod -R ugo-wx / |
|
Back to top |
|
 |
kimbert |
Posted: Mon Aug 24, 2015 2:30 pm Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
I think the OP's XPath looks OK. Only the root element has a namespace - the child elements are not in any namespace. So I don't think that's the problem.
The /XMLNSC at the start is wrong - MbXPath assumes that the document root is the first child of the domain. So the path should be '/otns:dequeue2Response/dequeue2Return/count'. _________________ Before you criticize someone, walk a mile in their shoes. That way you're a mile away, and you have their shoes too. |
|
Back to top |
|
 |
|