|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
XPath in Java returns List object for text() node ? |
« View previous topic :: View next topic » |
Author |
Message
|
Cogito-Ergo-Sum |
Posted: Mon Apr 11, 2011 11:38 pm Post subject: XPath in Java returns List object for text() node ? |
|
|
 Master
Joined: 07 Feb 2006 Posts: 293 Location: Bengaluru, India
|
I would like to know, whether it is incorrect to expect a String object instead of a List object from a text() node in an XPath expression from evaluateXPath(MbXPath) ? Conversely, in what circumstances, can evaluateXPath(MbXPath) return a String or say, a Double as mentioned in the javadoc ?
With this code :
Code: |
mbx = new MbXPath("/a/b/c/text()");
mbx.addNamespacePrefix("tns", "example.com") ;
String s = (String) inMsg.evaluateXPath(mbx) ; |
I get an exception as shown below :
Code: |
Apr 11 22:50:19 nsubrahm WebSphere Broker v7001[4552]: (NSUBRAHM.BRKR.default)[8]BIP4367E: The method 'evaluate' in Java node 'JC01' has thrown the following exception: [b]java.lang.ClassCastException: java.util.Arrays$ArrayList incompatible with java.lang.String[/b]. : NSUBRAHM.BRKR.9ea86cb8-2e01-0000-0080-ec35aac0b0a5: /build/S700_P/src/DataFlowEngine/PluginInterface/com_ibm_broker_plugin_CMbService.cpp: 1845: ImbJavaExceptionUtils::throwableToNativeException: : |
_________________ ALL opinions are welcome.
-----------------------------
Debugging tip:When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
---Sherlock Holmes |
|
Back to top |
|
 |
kimbert |
Posted: Tue Apr 12, 2011 11:46 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
I expect it depends on whether the XPath expression matched a single node or a list of nodes. And if a single node was matched, I would expect a simple data type for a node with a value, but a list otherwise.
If you posted your XPath expression and the output of a Trace node then I could be more specific. |
|
Back to top |
|
 |
rekarm01 |
Posted: Tue Apr 12, 2011 6:02 pm Post subject: Re: XPath in Java returns List object for text() node ? |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 1415
|
Cogito-Ergo-Sum wrote: |
I would like to know, whether it is incorrect to expect a String object instead of a List object from a text() node in an XPath expression from evaluateXPath(MbXPath)? |
Yes, it is incorrect to expect that. text() always returns a node-set, regardless of the number of matching text nodes.
Cogito-Ergo-Sum wrote: |
Conversely, in what circumstances, can evaluateXPath(MbXPath) return a String or say, a Double as mentioned in the javadoc? |
The javadoc also provides a link to the w3c recommendation for XPath 1.0, which specifies exactly how to evaluate XPath expressions. |
|
Back to top |
|
 |
Cogito-Ergo-Sum |
Posted: Tue Apr 12, 2011 6:52 pm Post subject: |
|
|
 Master
Joined: 07 Feb 2006 Posts: 293 Location: Bengaluru, India
|
Quote: |
text() always returns a node-set, regardless of the number of matching text nodes. |
oh....yes, now I understand. For example, I used this document
Code: |
<root><tag1><tag2>This is some data of <date>Apr-13-2011</date> as shown in this document.</tag2></tag1></root> |
and applied XPath as
Code: |
/root/tag1/tag2/text() |
which returns two text() nodes as
Code: |
This is some data of
as shown in this document |
Thanks ! _________________ ALL opinions are welcome.
-----------------------------
Debugging tip:When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
---Sherlock Holmes |
|
Back to top |
|
 |
McueMart |
Posted: Thu Dec 13, 2012 11:07 am Post subject: |
|
|
 Chevalier
Joined: 29 Nov 2011 Posts: 490 Location: UK...somewhere
|
Just digging this post up so it might help someone in future....
As the OP found out, selecting the text node using text() doesn't cause evaluateXPath(MbXPath) to return a String. If you want it to return a string you can do something like:
Code: |
mbx = new MbXPath("string(/root/tag1/tag2)");
String s = (String) inMsg.evaluateXPath(mbx) ;
|
I appreciate this is just basic xpath but it took me more than a couple of minutes to figure it out  |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|