Author |
Message
|
DevNYC1 |
Posted: Tue Oct 16, 2012 12:58 pm Post subject: Problem while parsing XML in a Java Node |
|
|
Apprentice
Joined: 22 Aug 2012 Posts: 32
|
I am trying to create a simple example of a Java Node that parses a simple XML message. I was not able to make it work - I get empty lists and null values when I try to do the parsing consistent with the code I could find.
I looked through this forum and IBM documentation. The following topic seems to suggest a working example: http://www.mqseries.net/phpBB/viewtopic.php?t=51245
Here is my simple input XML:
Code: |
<?xml version="1.0" encoding="UTF-8"?>
<Student>
<ID>120</ID>
<Name>James</Name>
<English>95</English>
<Math>40</Math>
</Student>
<Student>
<ID>149</ID>
<Name>Peter</Name>
<English>88</English>
<Math>52</Math>
</Student> |
Here is the fragment of my Java Code:
Code: |
public void evaluate(MbMessageAssembly assembly) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
MbMessage inMessage = assembly.getMessage();
MbElement inRoot = inMessage.getRootElement();
MbElement inputBody = inRoot.getLastChild();
// All these 3 lists come back empty (size == 0)
List studentList = (List)inputBody.evaluateXPath("./Student");
Log.as_looger.info("studentList size: " + studentList.size());
List studentList2 = (List)inputBody.evaluateXPath("//Student");
Log.as_looger.info("studentList2 size: " + studentList2.size());
List studentList3 = (List)inputBody.evaluateXPath("/Student");
Log.as_looger.info("studentList3 size: " + studentList3.size());
// returns null value
MbElement StudentElem = inRoot.getFirstElementByPath("Student"); |
Any ideas? |
|
Back to top |
|
 |
mqjeff |
Posted: Tue Oct 16, 2012 1:19 pm Post subject: Re: Problem while parsing XML in a Java Node |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
DevNYC1 wrote: |
Here is my simple input XML:
Code: |
<?xml version="1.0" encoding="UTF-8"?>
<Student>
<ID>120</ID>
<Name>James</Name>
<English>95</English>
<Math>40</Math>
</Student>
<Student>
<ID>149</ID>
<Name>Peter</Name>
<English>88</English>
<Math>52</Math>
</Student> |
|
That's, unfortunately, not a valid XML document.
It's two valid XML documents stuck together as if it were one document.
You would need to wrap it in something like
Code: |
<Students>...</Students> |
such that there is exactly one root element. |
|
Back to top |
|
 |
DevNYC1 |
Posted: Tue Oct 16, 2012 1:31 pm Post subject: Re: Problem while parsing XML in a Java Node |
|
|
Apprentice
Joined: 22 Aug 2012 Posts: 32
|
mqjeff wrote: |
That's, unfortunately, not a valid XML document.
It's two valid XML documents stuck together as if it were one document |
Thanks for your suggestion. I changed the XML document to the following:
Code: |
<?xml version="1.0" encoding="UTF-8"?>
<Students>
<Student>
<ID>120</ID>
<Name>James</Name>
<English>95</English>
<Math>40</Math>
</Student>
<Student>
<ID>149</ID>
<Name>Peter</Name>
<English>88</English>
<Math>52</Math>
</Student>
</Students> |
And updated the Java code as follows. But I still have the same problem - the empty lists returned and getFirstElementByPath returns null:
Code: |
MbMessage inMessage = assembly.getMessage();
MbElement inRoot = inMessage.getRootElement();
MbElement inputBody = inRoot.getLastChild();
// All 3 lists are empty (length == 0)
List studentList = (List)inputBody.evaluateXPath("./Students/Student");
Log.as_looger.info("studentList size: " + studentList.size());
List studentList2 = (List)inputBody.evaluateXPath("//Student");
Log.as_looger.info("studentList2 size: " + studentList2.size());
List studentList3 = (List)inputBody.evaluateXPath("/Students/Student");
Log.as_looger.info("studentList3 size: " + studentList3.size());
// returns null value
MbElement StudentElem = inRoot.getFirstElementByPath("Student"); |
|
|
Back to top |
|
 |
marko.pitkanen |
Posted: Wed Oct 17, 2012 1:56 am Post subject: |
|
|
 Chevalier
Joined: 23 Jul 2008 Posts: 440 Location: Jamsa, Finland
|
Hi,
A simple question, do you somewhere before jnc assosiate XMLNSC parser to you message or is it handled as a BLOB?
--
Marko |
|
Back to top |
|
 |
DevNYC1 |
Posted: Wed Oct 17, 2012 6:26 am Post subject: |
|
|
Apprentice
Joined: 22 Aug 2012 Posts: 32
|
marko.pitkanen wrote: |
Hi,
A simple question, do you somewhere before jnc assosiate XMLNSC parser to you message or is it handled as a BLOB?
--
Marko |
I created the new Flow Project from scratch and which created the default message set with the XMLNSC parser mapping. I did not change anything there.
In fact, to test that I did not change something inadvertently I created the new flow project again, put together the Input, Java Compute and Output node and added the code I used in this question. The same problem remains. |
|
Back to top |
|
 |
Esa |
Posted: Wed Oct 17, 2012 6:51 am Post subject: |
|
|
 Grand Master
Joined: 22 May 2008 Posts: 1387 Location: Finland
|
DevNYC1 wrote: |
I created the new Flow Project from scratch and which created the default message set with the XMLNSC parser mapping. I did not change anything there.
|
That means that the name of InputBody is 'MRM'.
Either change MQInput to use XMLNSC of try this:
Code: |
List studentList = (List)inputBody.evaluateXPath("Student"); |
|
|
Back to top |
|
 |
DevNYC1 |
Posted: Wed Oct 17, 2012 8:17 am Post subject: |
|
|
Apprentice
Joined: 22 Aug 2012 Posts: 32
|
Esa wrote: |
That means that the name of InputBody is 'MRM'.
Either change MQInput to use XMLNSC of try this |
Exactly! Issue solved. Thanks to you and Marko for the tip. |
|
Back to top |
|
 |
kimbert |
Posted: Thu Oct 18, 2012 2:44 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
Quote: |
Exactly! Issue solved. Thanks to you and Marko for the tip. |
I don't agree. The real 'issue' here is why the OP didn't know how to diagnose this. A Trace node or a user trace would have revealed the root cause. |
|
Back to top |
|
 |
|