Need help - extract node using JAVA Xpath

B

brahatha

I am trying to come up with a Java code where I can read a xml file
and extract particular element/attribute. This is the structure of
xml
I have

<RootElement>
<colAttempt>
<Attempt a="1" b="abc>
<ResultElement Result = "123" Msg="success">
<nodeList>
<NodeIwant nodeName="conf" noFail="1">
<Data ProgId="294"/>
</NodeIwant>
<NodeIwant nodeName="nconf" noFail="0">
<Data ProgId="3"/>
</NodeIwant>
</nodeList>
</ResultElement>


</Attempt>
<Attempt c="2" d="abc>
<ResultElement Result = "123" Msg="success">
<nodeList>
<NodeIwant nodeName="xyz" noFail="0">
<Data ProgId="4"/>
</NodeIwant>
</nodeList>
</ResultElement>


</Attempt>


</colAttempt>
</RootElement>


I need to extract <NodeIwant nodeName="" noFail =""> for each
iteration of Attempt.In the above example, I have two Attempts ( 1
and
2 ). So my output should show, for attempt 1 following result
<NodeIwant nodeName="conf" noFail="1">
<NodeIwant nodeName="nconf" noFail="0">


For attempt 2 following
<NodeIwant nodeName="xyz" noFail="0">


So final result should be like this,


Attempt 1:
<NodeIwant nodeName="conf" noFail="1">
<NodeIwant nodeName="nconf" noFail="0">
Attempt 2:
<NodeIwant nodeName="xyz" noFail="0">


NEED HELP!
 
B

brahatha

I am trying to come up with a Java code where I can read a xml file
and extract particular element/attribute. This is the structure of
xml
I have

<RootElement>
<colAttempt>
<Attempt a="1" b="abc>
<ResultElement Result = "123" Msg="success">
<nodeList>
<NodeIwant nodeName="conf" noFail="1">
<Data ProgId="294"/>
</NodeIwant>
<NodeIwant nodeName="nconf" noFail="0">
<Data ProgId="3"/>
</NodeIwant>
</nodeList>
</ResultElement>

</Attempt>
<Attempt c="2" d="abc>
<ResultElement Result = "123" Msg="success">
<nodeList>
<NodeIwant nodeName="xyz" noFail="0">
<Data ProgId="4"/>
</NodeIwant>
</nodeList>
</ResultElement>

</Attempt>

</colAttempt>
</RootElement>

I need to extract <NodeIwant nodeName="" noFail =""> for each
iteration of Attempt.In the above example, I have two Attempts ( 1
and
2 ). So my output should show, for attempt 1 following result
<NodeIwant nodeName="conf" noFail="1">
<NodeIwant nodeName="nconf" noFail="0">

For attempt 2 following
<NodeIwant nodeName="xyz" noFail="0">

So final result should be like this,

Attempt 1:
<NodeIwant nodeName="conf" noFail="1">
<NodeIwant nodeName="nconf" noFail="0">
Attempt 2:
<NodeIwant nodeName="xyz" noFail="0">

NEED HELP!

I tried this as suggested by Martin Honnen,
/RootElement/colAttempt/*[starts-with(name(),'Attempt')]/
ResultElement/nodeList/NodeIwant
I get NPE.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top