Author |
Message
|
MB Developer |
Posted: Mon Apr 06, 2015 5:52 am Post subject: Sorting XML data using JavaCompute Node |
|
|
 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 |
|
 |
mqjeff |
Posted: Mon Apr 06, 2015 6:05 am Post subject: Re: Sorting XML data using JavaCompute Node |
|
|
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 |
|
 |
MB Developer |
Posted: Mon Apr 06, 2015 7:28 am Post subject: |
|
|
 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 |
|
 |
mqjeff |
Posted: Mon Apr 06, 2015 8:36 am Post subject: |
|
|
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 |
|
 |
MB Developer |
Posted: Mon Apr 06, 2015 9:11 am Post subject: |
|
|
 Disciple
Joined: 03 Feb 2014 Posts: 179
|
Hi mqjeff,
let me know how to apply sorting code.... _________________ Thanks.... |
|
Back to top |
|
 |
mqjeff |
Posted: Mon Apr 06, 2015 9:23 am Post subject: |
|
|
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 |
|
 |
MB Developer |
Posted: Mon Apr 06, 2015 10:12 am Post subject: |
|
|
 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 |
|
 |
mqjeff |
Posted: Mon Apr 06, 2015 10:51 am Post subject: |
|
|
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 |
|
 |
MB Developer |
Posted: Wed Apr 08, 2015 9:51 am Post subject: |
|
|
 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 |
|
 |
mqjeff |
Posted: Wed Apr 08, 2015 9:56 am Post subject: |
|
|
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 |
|
 |
MB Developer |
Posted: Wed Apr 08, 2015 9:59 am Post subject: |
|
|
 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 |
|
 |
MB Developer |
Posted: Wed Apr 08, 2015 10:08 am Post subject: |
|
|
 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 |
|
 |
fjb_saper |
Posted: Wed Apr 08, 2015 8:04 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 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 |
|
 |
|