Author |
Message
|
satquick |
Posted: Wed May 28, 2014 12:27 pm Post subject: Using Xpath in a JCN in an MBElement Returning a Null value |
|
|
Newbie
Joined: 28 May 2014 Posts: 4
|
Hi Everyone.
I am new in this forum and recently I got a big dilemma with this:
using a JCN
The flow is as follows:
MQInput ==> Trace ==> JNC ==> Trace==>MQOutput
I added these 2 TRace Nodes in order to track the values in the original Message by using the Correlation Name ${Root}.
Now this is the issue in question:
//1.-Xpath expresion to get the first customer in the XML
List customer = (List)inMessage.evaluateXPath("customers/customer[1]");
//2.-obtaining the first element contained in the list
MbElement customerElement= (MbElement)customer.get(0);
//3.-using the method getFirstElementByPath which one returns the value
String nameByFirstElement = customerElement.getFirstElementByPath("name").getValueAsString();
//4.-using the MBElement this Xpath Evaluation fails and returns null
String nameByXPath = (String)customerElement.evaluateXPath("string(/customer/name/text())");
This is the XML used:
Code: |
<customers>
<customer id="12345">
<name>George </name>
<LastName>Lopez</LastName>
<address>
<street>Av. Inventada #10</street>
<city></city>
<state></state>
<country>Mexico</country>
</address>
</customer>
<customer id="123456">
<name>Adrian</name>
<LastName>Lopez</LastName>
<address>
<street>Av. Inventada #10</street>
<city>Zapopan</city>
<state>Jalisco</state>
<country>Mexico</country>
</address>
</customer>
</customers> |
The first XPath in point number 1 returns correctly the first set of the XML
Code: |
<customers>
<customer id="12345">
<name>George </name>
<LastName>Lopez</LastName>
<address>
<street>Av. Inventada #10</street>
<city></city>
<state></state>
<country>Mexico</country>
</address>
</customer> |
point number 3 works correctly using the method getFirstElementByPath in MbElement the value of name is returned , but when I try to get the same value using XPATH in MBELement I got a null Value!!
String nameByXPath = (String)customerElement.evaluateXPath("string(/customer/name/text())");
what is the correct XPATH expresion that should I need to use ? or Am doing something wrong?
Please need your advice!!! |
|
Back to top |
|
 |
fjb_saper |
Posted: Wed May 28, 2014 12:55 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
I don't think having "string" is the xpath is correct...
Have you tried:
Code: |
String nameByXPath = (String)customerElement.evaluateXPath("/customer[1]/name[1]/text()"); |
Have fun  _________________ MQ & Broker admin |
|
Back to top |
|
 |
kimbert |
Posted: Wed May 28, 2014 1:41 pm Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
Quote: |
I don't think having "string" is the xpath is correct... |
It looks like an XPath 'constructor' for xs:string. Whether it is correct depends on whether the XML Schema namespace ( usual prefix 'xs' or 'xsd' ) has been bound to the empty prefix in the namespace settings associated with the query. _________________ 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 |
|
 |
ganesh |
Posted: Wed May 28, 2014 3:11 pm Post subject: |
|
|
Master
Joined: 18 Jul 2010 Posts: 294
|
Quote: |
what is the correct XPATH expresion that should I need to use ? or Am doing something wrong? |
There are several tools that can give you an xpath of an element, you might even find a free online tool to get xpath's. |
|
Back to top |
|
 |
satquick |
Posted: Wed May 28, 2014 4:28 pm Post subject: |
|
|
Newbie
Joined: 28 May 2014 Posts: 4
|
Hi jfb_saper ! I have already tried and in the way that you indicated it, I need to receive the response in an ArrayList as follows:
List nameValue =(List)inMessage.evaluateXPath("customers/customer[1]/name[1]/text()");
I got null either =) by the way I am using WMBT 8.0.0.1
fjb_saper wrote: |
I don't think having "string" is the xpath is correct...
Have you tried:
Code: |
String nameByXPath = (String)customerElement.evaluateXPath("/customer[1]/name[1]/text()"); |
Have fun  |
|
|
Back to top |
|
 |
satquick |
Posted: Wed May 28, 2014 4:46 pm Post subject: |
|
|
Newbie
Joined: 28 May 2014 Posts: 4
|
ganesh wrote: |
Quote: |
what is the correct XPATH expresion that should I need to use ? or Am doing something wrong? |
There are several tools that can give you an xpath of an element, you might even find a free online tool to get xpath's. |
I did some modifications and now I have this:
Code: |
//taking the String value directly from the Message Assembly this works!
String nameValue=(String)inMessage.evaluateXPath("string(customers/customer[1]/name)");
//Xpath expresion to get the first customer in the XML
List customer = (List)inMessage.evaluateXPath("customers/customer[1]");
//obtaining the first element contained in the list
MbElement customerElement= (MbElement)customer.get(0);
//using the MBElement this Xpath Evaluation fails and returns null this does not work!
String nameByXPath = (String)customerElement.evaluateXPath("string(customer/name)");
|
If I try to get the name value directly from the Message Assembly then I get the value using XPATH(this works ):
Code: |
String nameValue =(String)inMessage.evaluateXPath("string(customers/customer[1]/name)"); |
but If I try it to get the value from a MbMessage created on the XPath before it returns null value even if in the debug mode it says that the MbElement contains data.
Code: |
//Xpath expresion to get the first customer in the XML
List customer = (List)inMessage.evaluateXPath("customers/customer[1]");
//obtaining the first element contained in the list
MbElement customerElement= (MbElement)customer.get(0);
//using the MBElement this Xpath Evaluation fails and returns null this does not work!
String nameValue=(String)inMessage.evaluateXPath("string(customer/name)"); |
Also if I try to convert the Body MbElement to String this does work as well:
Code: |
//obtaining the XMLNSC DOMAIN that represents the XML
MbElement xmlnsc = inMessage.getRootElement().getLastChild();
//Converting the XMLNSC to String
String value= new String((byte[])xmlnsc.toBitstream(null, null, null, 0, 0, 0));
|
but If I try to convert the MbElement created using the Xpath I got a beautiful Message: Unable to resolve message exception:
Quote: |
(0x01000000:Name ):Insert = (
(0x03000000:NameValue):Type = 5 (INTEGER)
(0x03000000:NameValue):Text = '<com.ibm.broker.plugin.MbUserException class:com.ibm.customers.Customer_MessageFlow_JavaCompute method:evaluate() source: key: message: Unable to resolve message: Source: Key: >' (CHARACTER)
) |
Code: |
//List customer = (List)inMessage.evaluateXPath("customers/customer[1]");
List customer = (List)inMessage.evaluateXPath("customers/customer[1]");
//obtaining the first element contained in the list
MbElement customerElement= (MbElement)customer.get(0);
//convert it to String
String customerElementValue =new String((byte[])customerElement.toBitstream(null, null, null, 0, 0, 0)); |
|
|
Back to top |
|
 |
mqjeff |
Posted: Mon Jun 02, 2014 8:34 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
Your second xpath that returns null is looking for the equivalent of /customers/customer[1]/customer/name
That's why it returns null. |
|
Back to top |
|
 |
satquick |
Posted: Wed Jun 11, 2014 12:41 pm Post subject: |
|
|
Newbie
Joined: 28 May 2014 Posts: 4
|
mqjeff wrote: |
Your second xpath that returns null is looking for the equivalent of /customers/customer[1]/customer/name
That's why it returns null. |
Thanks mqjeff! yeah !! that worked!
This is the correct way:
Code: |
String nameByXPath = (String)customerElement.evaluateXPath("string(name)");
|
This is the complete code:
Code: |
//Xpath expresion to get the first customer in the XML
//List customer = (List)inMessage.evaluateXPath("customers/customer[1]");
String nameValue =(String)inMessage.evaluateXPath("string(customers/customer[1]/name)");
//List customer = (List)inMessage.evaluateXPath("customers/customer[1]");
List customer = (List)xmlnsc.evaluateXPath("customers/customer[1]");
//obtaining the first element contained in the list
MbElement customerElement= (MbElement)customer.get(0);
//using the method getFirstElementByPath which one returns the value
String nameByFirstElement = customerElement.getFirstElementByPath("name").getValueAsString();
//using the MBElement this Xpath Evaluation fails and returns null
String nameByXPath = (String)customerElement.evaluateXPath("string(name)"); |
[/code] |
|
Back to top |
|
 |
|