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 in accessing List using XPATH

Post new topic  Reply to topic Goto page 1, 2  Next
 Problem in accessing List using XPATH « View previous topic :: View next topic » 
Author Message
SDS
PostPosted: Thu Mar 01, 2012 11:57 pm    Post subject: Problem in accessing List using XPATH Reply with quote

Apprentice

Joined: 13 Jun 2011
Posts: 47

I have an input message like below

Code:
<Tx>
<retailTx>

  <LineItem VoidFlag="false">
  <SequenceNumber>30</SequenceNumber>
  <Membership>
  <MembershipID>11941590607</MembershipID>
  <KeyedOrScanned>K</KeyedOrScanned>
  <SaleOrReturn>S</SaleOrReturn>
  </Membership>
  </LineItem>

  <LineItem VoidFlag="false">
  <SequenceNumber>40</SequenceNumber>
  <Sale ItemType="Stock">
  <POSIdentity>
  <POSItemID>00004300003345</POSItemID>
  </POSIdentity>
  <ExtendedAmount>5.99</ExtendedAmount>
  <Quantity>1</Quantity> 
  </LineItem>

</Tx>
</retailTx>


I have to access the different LineItem values using XPATH and Java.
How can I do it???

I have tried to use the following statement
Code:
for (MbElement refLineItem:  List<MbElement>)inputBody.evaluateXPath("./Tx/retailTx/LineItem"))
{
 String XSeqNo = (refLineItem.getFirstElementByPath("SequenceNumber")).getValueAsString();
}


But its showing error in for loop as "Can only iterate over an array or an instance of java.lang.Iterable".

Can anyone help to resolve this issue?
Back to top
View user's profile Send private message
Esa
PostPosted: Fri Mar 02, 2012 12:10 am    Post subject: Reply with quote

Grand Master

Joined: 22 May 2008
Posts: 1387
Location: Finland

For the first, your code cannot possibly have compiled because there is a left parenthes missing in the first line...
Back to top
View user's profile Send private message
SDS
PostPosted: Fri Mar 02, 2012 12:20 am    Post subject: Reply with quote

Apprentice

Joined: 13 Jun 2011
Posts: 47

sorry it was a typo here but the problem is not with it.

Code:
for (MbElement refLineItem:  (List<MbElement>)inputBody.evaluateXPath("./Tx/retailTx/LineItem"))
{
 String XSeqNo = (refLineItem.getFirstElementByPath("SequenceNumber")).getValueAsString();
}
Back to top
View user's profile Send private message
mqsiuser
PostPosted: Fri Mar 02, 2012 1:16 am    Post subject: Reply with quote

Yatiri

Joined: 15 Apr 2008
Posts: 637
Location: Germany

SDS wrote:
Code:
for (MbElement refLineItem:  (List<MbElement>)inputBody.evaluateXPath("./Tx/retailTx/LineItem"))
{
 String XSeqNo = (refLineItem.getFirstElementByPath("SequenceNumber")).getValueAsString();
}


try out "./SequenceNumber" instead of just "SequenceNumber"
_________________
Just use REFERENCEs
Back to top
View user's profile Send private message
vishnurajnr
PostPosted: Fri Mar 02, 2012 1:52 am    Post subject: Reply with quote

Centurion

Joined: 08 Aug 2011
Posts: 134
Location: Trivandrum

Try with below modification:

Code:
List <MbElement> Lineitem= (List <MbElement>)inputBody.evaluateXPath("./Tx/retailTx/LineItem");
         MbElement[] testarray = (MbElement[])Lineitem.toArray();
         
         for (MbElement refLineItem: testarray ){String XSeqNo = (refLineItem.getFirstElementByPath("./SequenceNumber")).getValueAsString(); }
Back to top
View user's profile Send private message Visit poster's website
adubya
PostPosted: Fri Mar 02, 2012 1:53 am    Post subject: Reply with quote

Partisan

Joined: 25 Aug 2011
Posts: 377
Location: GU12, UK

The error is with the iterator according to the first post so the OP needs to work out why

Code:
inputBody.evaluateXPath("./Tx/retailTx/LineItem")


is not working and yielding something which can be iterated over.
Back to top
View user's profile Send private message Send e-mail
adubya
PostPosted: Fri Mar 02, 2012 2:01 am    Post subject: Reply with quote

Partisan

Joined: 25 Aug 2011
Posts: 377
Location: GU12, UK

Code:
<Tx>
<retailTx>

  <LineItem VoidFlag="false">
  <SequenceNumber>30</SequenceNumber>
  <Membership>
  <MembershipID>11941590607</MembershipID>
  <KeyedOrScanned>K</KeyedOrScanned>
  <SaleOrReturn>S</SaleOrReturn>
  </Membership>
  </LineItem>

  <LineItem VoidFlag="false">
  <SequenceNumber>40</SequenceNumber>
  <Sale ItemType="Stock">
  <POSIdentity>
  <POSItemID>00004300003345</POSItemID>
  </POSIdentity>
  <ExtendedAmount>5.99</ExtendedAmount>
  <Quantity>1</Quantity> 
  </LineItem>

</Tx>
</retailTx>


Is bogus XML of course "Tx" is the parent element containing "retailTx" but the close tags are reversed. If that's not a typo then I guess that's your problem, garbage in, garbage out.
Back to top
View user's profile Send private message Send e-mail
vishnurajnr
PostPosted: Fri Mar 02, 2012 2:08 am    Post subject: Reply with quote

Centurion

Joined: 08 Aug 2011
Posts: 134
Location: Trivandrum

Quote:
mqsiuser
PostPosted: Fri Mar 02, 2012 1:16 am Post subject:
SDS wrote:
Code:
for (MbElement refLineItem: (List<MbElement>)inputBody.evaluateXPath("./Tx/retailTx/LineItem"))
{
String XSeqNo = (refLineItem.getFirstElementByPath("SequenceNumber")).getValueAsString();
}


try out "./SequenceNumber" instead of just "SequenceNumber"


This will make it work!
Back to top
View user's profile Send private message Visit poster's website
mqsiuser
PostPosted: Fri Mar 02, 2012 2:13 am    Post subject: Reply with quote

Yatiri

Joined: 15 Apr 2008
Posts: 637
Location: Germany

vishnurajnr wrote:
This will make it work!



You should start using a real XML editor (and sometimes press the "pretty print" button). You can download and freely use XMLPad.

There is build in support for XML(-editing) in Eclipse... well... probably you can use that, or likely use an external "dedicated" tool (you can/might spend money on XMLSpy or oXygen).
_________________
Just use REFERENCEs
Back to top
View user's profile Send private message
SDS
PostPosted: Sun Mar 04, 2012 11:42 pm    Post subject: Reply with quote

Apprentice

Joined: 13 Jun 2011
Posts: 47

Hi vishnurajnr,

I am using the following code but the control is not entering to the for loop.

Code:
List <MbElement> Lineitem= (List <MbElement>)inputBody.evaluateXPath("./Tx/retailTx/LineItem");
         MbElement[] testarray = (MbElement[])Lineitem.toArray();
         
         for (MbElement refLineItem: testarray ){String XSeqNo = (refLineItem.getFirstElementByPath("./SequenceNumber")).getValueAsString(); }


Can anyone resolve this issue?
Back to top
View user's profile Send private message
vishnurajnr
PostPosted: Mon Mar 05, 2012 1:44 am    Post subject: Reply with quote

Centurion

Joined: 08 Aug 2011
Posts: 134
Location: Trivandrum

Hi SDS,

Your First code it self will do the needful if you have incorporated './SequenceNumber'.
Have you tried mqsisuer's suggestion (./SequenceNumber)?

Are you getting any error before the for loop?

Or Have you imported "java.util.List"?
Back to top
View user's profile Send private message Visit poster's website
kimbert
PostPosted: Mon Mar 05, 2012 1:58 am    Post subject: Reply with quote

Jedi Council

Joined: 29 Jul 2003
Posts: 5542
Location: Southampton

I suggest that you change your code to this:
Code:
List<MbElement> queryResult = (List<MbElement>)inputBody.evaluateXPath("./Tx/retailTx/LineItem"));
if ( queryResult != null ) {
    for (MbElement refLineItem:  (queryResult )
    {
     String XSeqNo = (refLineItem.getFirstElementByPath("SequenceNumber")).getValueAsString();
    }
}

...because I suspect that your query is returning either a null or something that is not compatible with a for loop.
Back to top
View user's profile Send private message
SDS
PostPosted: Mon Mar 05, 2012 1:59 am    Post subject: Reply with quote

Apprentice

Joined: 13 Jun 2011
Posts: 47

@vishnurajnr,
I have used both the cases but the control is not entering to the for loop at all and it's not throwing any exception either.
"java.util.List" has been imported also.
Back to top
View user's profile Send private message
vishnurajnr
PostPosted: Mon Mar 05, 2012 2:14 am    Post subject: Reply with quote

Centurion

Joined: 08 Aug 2011
Posts: 134
Location: Trivandrum

Bit Confusing.., the below piece of code is a working version with your sample XML:

Code:
MbMessage outMessage = new MbMessage(inMessage);      

      try {
         MbElement outRoot = outMessage.getRootElement();
         MbElement inputBody = outRoot.getLastChild();
for (MbElement refLineItem:List<MbElement>)inputBody.evaluateXPath("./Tx/retailTx/LineItem"))
         {
          String XSeqNo = (refLineItem.getFirstElementByPath("./SequenceNumber")).getValueAsString();
         
         }

   MbMessageAssembly outAssembly = new MbMessageAssembly(contact admin,
               outMessage);
         out.propagate(outAssembly);

      }


It is iterating perfectly and the value is populating in XSeqNo everytime!
Double check once again.

Back to top
View user's profile Send private message Visit poster's website
SDS
PostPosted: Mon Mar 05, 2012 2:14 am    Post subject: Reply with quote

Apprentice

Joined: 13 Jun 2011
Posts: 47

@kimbert.

I have used your suggestion.
in this case the control is entering to the if block satisfying "( queryResult != null )" but while executing the for loop statement the control skips the whole loop.

God knows why it is not working!
It's not throwing any exception either.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Goto page 1, 2  Next Page 1 of 2

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Problem in accessing List using XPATH
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.