Need help to 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>
<Attempt1 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>

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

</Attempt2>

</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!
 
M

Martin Honnen

brahatha said:
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">

XPath 1.0 selects nodes in a document, it does not change them. Thus if
you select a NodeIwant element node in the XML sample you posted then it
continues to have its child nodes like those Data elements. If you only
need the NodeIwant elements without their child nodes then you need an
XSLT transformation.

As for the XPath
/RootElement/colAttempt/*[starts-with(name(),
'Attempt')]/ResultElement/nodeList/NodeIwant
should do to select all those NodeIwant elements, if you want to do it
in two steps then first select
/RootElement/colAttempt/Attempt1
and
/RootElement/colAttempt/Attempt2
and then relative to that
ResultElement/nodeList/NodeIwant
 

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
474,008
Messages
2,570,269
Members
46,869
Latest member
TresaMcGir

Latest Threads

Top