Author |
Message
|
SDS |
Posted: Thu Mar 01, 2012 11:57 pm Post subject: Problem in accessing List using XPATH |
|
|
 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 |
|
 |
Esa |
Posted: Fri Mar 02, 2012 12:10 am Post subject: |
|
|
 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 |
|
 |
SDS |
Posted: Fri Mar 02, 2012 12:20 am Post subject: |
|
|
 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 |
|
 |
mqsiuser |
Posted: Fri Mar 02, 2012 1:16 am Post subject: |
|
|
 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 |
|
 |
vishnurajnr |
Posted: Fri Mar 02, 2012 1:52 am Post subject: |
|
|
 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 |
|
 |
adubya |
Posted: Fri Mar 02, 2012 1:53 am Post subject: |
|
|
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 |
|
 |
adubya |
Posted: Fri Mar 02, 2012 2:01 am Post subject: |
|
|
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 |
|
 |
vishnurajnr |
Posted: Fri Mar 02, 2012 2:08 am Post subject: |
|
|
 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 |
|
 |
mqsiuser |
Posted: Fri Mar 02, 2012 2:13 am Post subject: |
|
|
 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 |
|
 |
SDS |
Posted: Sun Mar 04, 2012 11:42 pm Post subject: |
|
|
 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 |
|
 |
vishnurajnr |
Posted: Mon Mar 05, 2012 1:44 am Post subject: |
|
|
 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 |
|
 |
kimbert |
Posted: Mon Mar 05, 2012 1:58 am Post subject: |
|
|
 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 |
|
 |
SDS |
Posted: Mon Mar 05, 2012 1:59 am Post subject: |
|
|
 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 |
|
 |
vishnurajnr |
Posted: Mon Mar 05, 2012 2:14 am Post subject: |
|
|
 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 |
|
 |
SDS |
Posted: Mon Mar 05, 2012 2:14 am Post subject: |
|
|
 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 |
|
 |
|