ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Problem while parsing XML in a Java Node

Post new topic  Reply to topic
 Problem while parsing XML in a Java Node « View previous topic :: View next topic » 
Author Message
DevNYC1
PostPosted: Tue Oct 16, 2012 12:58 pm    Post subject: Problem while parsing XML in a Java Node Reply with quote

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
View user's profile Send private message
mqjeff
PostPosted: Tue Oct 16, 2012 1:19 pm    Post subject: Re: Problem while parsing XML in a Java Node Reply with quote

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
View user's profile Send private message
DevNYC1
PostPosted: Tue Oct 16, 2012 1:31 pm    Post subject: Re: Problem while parsing XML in a Java Node Reply with quote

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
View user's profile Send private message
marko.pitkanen
PostPosted: Wed Oct 17, 2012 1:56 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
DevNYC1
PostPosted: Wed Oct 17, 2012 6:26 am    Post subject: Reply with quote

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
View user's profile Send private message
Esa
PostPosted: Wed Oct 17, 2012 6:51 am    Post subject: Reply with quote

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
View user's profile Send private message
DevNYC1
PostPosted: Wed Oct 17, 2012 8:17 am    Post subject: Reply with quote

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
View user's profile Send private message
kimbert
PostPosted: Thu Oct 18, 2012 2:44 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Problem while parsing XML in a Java Node
Jump to:  



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
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.