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 » Sorting XML data using JavaCompute Node

Post new topic  Reply to topic
 Sorting XML data using JavaCompute Node « View previous topic :: View next topic » 
Author Message
MB Developer
PostPosted: Mon Apr 06, 2015 5:52 am    Post subject: Sorting XML data using JavaCompute Node Reply with quote

Disciple

Joined: 03 Feb 2014
Posts: 179

Hi All,
Greetings

MsgFlow: MQInput --> JavaCompute --> MQOutput


Input Message : JAVA.IN

Code:
<Items>
   <ItemLoc>
      <location>5</location>
      <class>2</class>
      <item>2</item>
   </ItemLoc>
   <ItemLoc>
         <location>5</location>
         <class>1</class>
         <item>1</item>         
   </ItemLoc>
   <ItemLoc>
         <location>2</location>
         <class>6</class>
         <item>10</item>         
   </ItemLoc>
   <ItemLoc>
         <location>2</location>
         <class>7</class>
         <item>11</item>         
   </ItemLoc>
   <ItemLoc>
         <location>2</location>
         <class>7</class>
         <item>5</item>         
   </ItemLoc>
</Items>


I want to sort all the records based on the location in ItemLoc ,So my output message like below
Code:
<Items>
   <ItemLoc>
      <location>2</location>
      <class>6</class>
      <item>10</item>
   </ItemLoc>
   <ItemLoc>
         <location>2</location>
         <class>7</class>
         <item>5</item>         
   </ItemLoc>
   <ItemLoc>
         <location>2</location>
         <class>7</class>
         <item>11</item>         
   </ItemLoc>
   <ItemLoc>
         <location>5</location>
         <class>1</class>
         <item>1</item>         
   </ItemLoc>
   <ItemLoc>
         <location>5</location>
         <class>2</class>
         <item>2</item>         
   </ItemLoc>
</Items>   



This is my requirement for that i will right code in java compute node I will take Java class


MbElement itemEle = inMessage.getRootElement().getLastChild().getFirstChild().getFirstChild();


1. Which java class is used for list all the ItemLoc

Please send me some sample code for sorting as per my requirement .
_________________
Thanks....
Back to top
View user's profile Send private message
mqjeff
PostPosted: Mon Apr 06, 2015 6:05 am    Post subject: Re: Sorting XML data using JavaCompute Node Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

MB Developer wrote:
This is my requirement for that i will right code in java compute node I will take Java class


MbElement itemEle = inMessage.getRootElement().getLastChild().getFirstChild().getFirstChild();


1. Which java class is used for list all the ItemLoc

The one you write for your JavaCompute node.

MB Developer wrote:
Please send me some sample code for sorting as per my requirement .

there isn't an inbuilt broker class that handles sorting.

You therefore need to handle the sorting yourself.

For your particular case, where you are looping over a set of input records, and creating a set of output records, as long as the set of input records is not significantly huge, I would use an insert sort - where each input record is inserted into the output tree between records that are "smaller" than it and records that are "bigger" than it.

Best of luck with your Google and Java documentation searches.
Back to top
View user's profile Send private message
MB Developer
PostPosted: Mon Apr 06, 2015 7:28 am    Post subject: Reply with quote

Disciple

Joined: 03 Feb 2014
Posts: 179

Hi mqjeff thanks for Reply..


I write below code in JavaCompute node

Quote:
MbElement outRoot = outMessage.getRootElement();
MbElement outBody = outRoot.createElementAsLastChild("XMLNSC");
MbElement outItems = outBody.createElementAsLastChild(MbXMLNSC.FOLDER,"Items",null);

MbElement itemEle = inMessage.getRootElement().getLastChild().getFirstChild().getFirstChild();
while (itemEle != null)
{

MbElement eitemChildEle = itemEle.getFirstChild();

MbElement eitemChildEle2 = itemEle.getFirstChild().getNextSibling();
MbElement eitemChildEle3 = itemEle.getLastChild();

String itemLoc = "";
String itemLocChildName = "";
itemLocChildName = eitemChildEle.getValueAsString();

int itemLocChildNamei =Integer.parseInt(itemLocChildName);


ArrayList<Integer> arraylist = new ArrayList<Integer>();
arraylist.add(itemLocChildNamei);
Collections.sort(arraylist);

String itemClsChildName = "";
itemClsChildName = eitemChildEle2.getValueAsString();

String itemitemChildName = "";
itemitemChildName = eitemChildEle3.getValueAsString();

MbElement outItems1 = outItems.createElementAsLastChild(MbXMLNSC.FIELD, "ItemLoc", itemLoc);
outItems1.createElementAsLastChild(MbXMLNSC.FIELD, "location", itemLocChildNamei);
outItems1.createElementAsLastChild(MbXMLNSC.FIELD, "class", itemClsChildName);
outItems1.createElementAsLastChild(MbXMLNSC.FIELD, "item", itemitemChildName);

itemEle = itemEle.getNextSibling();
}




But if I give input as my XML Items then same XML will coming...
_________________
Thanks....
Back to top
View user's profile Send private message
mqjeff
PostPosted: Mon Apr 06, 2015 8:36 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

MB Developer wrote:
But if I give input as my XML Items then same XML will coming...


Because that's what your code says to do.

You have successfully managed to sort the entries.

What are you doing to use that sort to create your output entries?
Back to top
View user's profile Send private message
MB Developer
PostPosted: Mon Apr 06, 2015 9:11 am    Post subject: Reply with quote

Disciple

Joined: 03 Feb 2014
Posts: 179

Hi mqjeff,

let me know how to apply sorting code....
_________________
Thanks....
Back to top
View user's profile Send private message
mqjeff
PostPosted: Mon Apr 06, 2015 9:23 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

MB Developer wrote:
Hi mqjeff,

let me know how to apply sorting code....


You have applied it.

You're not using it.

Look closely at your code.

Step through each part.

Look at the parts that create the output message.
Back to top
View user's profile Send private message
MB Developer
PostPosted: Mon Apr 06, 2015 10:12 am    Post subject: Reply with quote

Disciple

Joined: 03 Feb 2014
Posts: 179

Hi mqjeff,

I go through code again and again more closely but I am not getting.

please mqjeff give me some hint ...
_________________
Thanks....
Back to top
View user's profile Send private message
mqjeff
PostPosted: Mon Apr 06, 2015 10:51 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

You are using Java Collections to create and sort an array.

How are you using that array to populate the output tree?
Back to top
View user's profile Send private message
MB Developer
PostPosted: Wed Apr 08, 2015 9:51 am    Post subject: Reply with quote

Disciple

Joined: 03 Feb 2014
Posts: 179

Hi mqjeff,

send me the sample code .... I will try but I am not getting ..
_________________
Thanks....
Back to top
View user's profile Send private message
mqjeff
PostPosted: Wed Apr 08, 2015 9:56 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

MB Developer wrote:
Hi mqjeff,

send me the sample code .... I will try but I am not getting ..




Your code says
Code:
Collections.sort(arraylist);


Your code never uses that again.

What is that array list for, in your code?
Back to top
View user's profile Send private message
MB Developer
PostPosted: Wed Apr 08, 2015 9:59 am    Post subject: Reply with quote

Disciple

Joined: 03 Feb 2014
Posts: 179

Hi mqjeff,
Thanks for quick reply ...

yes I am also think at that line ..I don't know how to use in out put.
_________________
Thanks....
Back to top
View user's profile Send private message
MB Developer
PostPosted: Wed Apr 08, 2015 10:08 am    Post subject: Reply with quote

Disciple

Joined: 03 Feb 2014
Posts: 179

Hi mqjeff

This is my New code :

Code:
MbMessage inMessage = contact admin.getMessage();

      MbMessage outMessage = new MbMessage();
      MbMessageAssembly outAssembly = new MbMessageAssembly(contact admin,
            outMessage);

      try {
         copyMessageHeaders(inMessage, outMessage);
         // ----------------------------------------------------------
         // Add user code below
         MbElement outRoot = outMessage.getRootElement();
         MbElement outBody = outRoot.createElementAsLastChild("XMLNSC");
         MbElement outItems = outBody.createElementAsLastChild(MbXMLNSC.FOLDER,"Items",null);
         
         MbElement itemEle = inMessage.getRootElement().getLastChild().getFirstChild().getFirstChild();
         
         while (itemEle != null)
         {
            
            MbElement eitemChildEle = itemEle.getFirstChild();
            
            MbElement eitemChildEle2 = itemEle.getFirstChild().getNextSibling();
            MbElement eitemChildEle3 = itemEle.getLastChild();
            
            String itemLoc = "";
            String itemLocChildName = "";
            itemLocChildName = eitemChildEle.getValueAsString();
            
                                                   
            String itemClsChildName = "";
            itemClsChildName = eitemChildEle2.getValueAsString();
            
            String itemitemChildName = "";
            itemitemChildName = eitemChildEle3.getValueAsString();
            
              int itemitemChildNamei =Integer.parseInt(itemitemChildName);
            
         ArrayList<Integer> arraylist = new ArrayList<Integer>();
             arraylist.add(itemitemChildNamei);
             Collections.sort(arraylist);
            
            
                               
                                 
                /* Comparator<Integer> comparator = new Comparator<Integer>() {

                  @Override
                  public int compare(Integer o1, Integer o2) {
                     // TODO Auto-generated method stub
                     return o1.compareTo(o2) ;
                  }
                    
                 };
                 Collections.sort(children, comparator);*/
             
              MbElement outItems1 = outItems.createElementAsLastChild(MbXMLNSC.FIELD, "ItemLoc", itemLoc);
               outItems1.createElementAsLastChild(MbXMLNSC.FIELD, "location", itemLocChildName);
               outItems1.createElementAsLastChild(MbXMLNSC.FIELD, "class", itemClsChildName);
               outItems1.createElementAsLastChild(MbXMLNSC.FIELD, "item", itemitemChildNamei);
               
               itemEle = itemEle.getNextSibling();
            
         }



If I use that arrayList in output :

Quote:
MbElement outItems1 = outItems.createElementAsLastChild(MbXMLNSC.FIELD, "ItemLoc", itemLoc);
outItems1.createElementAsLastChild(MbXMLNSC.FIELD, "location", itemLocChildName);
outItems1.createElementAsLastChild(MbXMLNSC.FIELD, "class", itemClsChildName);
outItems1.createElementAsLastChild(MbXMLNSC.FIELD, "item",arraylist.itemitemChildNamei);


Then error will come " itemitemChildNamei cannot be resolved or is not a field "

OR
Quote:
MbElement outItems1 = outItems.createElementAsLastChild(MbXMLNSC.FIELD, "ItemLoc", itemLoc);
outItems1.createElementAsLastChild(MbXMLNSC.FIELD, "location", itemLocChildName);
outItems1.createElementAsLastChild(MbXMLNSC.FIELD, "class", itemClsChildName);
outItems1.createElementAsLastChild(MbXMLNSC.FIELD, "item",arraylist);

Than same xml file is getting sortin is not done
_________________
Thanks....
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Wed Apr 08, 2015 8:04 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20696
Location: LI,NY

Code:
  itemitemChildName = eitemChildEle3.getValueAsString();
             
              int itemitemChildNamei =Integer.parseInt(itemitemChildName);

It would be nice to see the String value of itemitemChildName.
You are using MbXMLNSC.FIELD a lot. I would have expected you to use instead MbXMLNSC.NAME or MbXMLNSC.NAME_VALUE instead...
and by the way a complex Java Object (like an array) does not constitute a legitimate value...
_________________
MQ & Broker admin
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Sorting XML data using JavaCompute 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.