0

I've one xml like:-

 <SkillMap>
   <ExitPoint ID="01">
    <NodeName>abcd</NodeName>
   </ExitPoint>
   <ExitPoint ID="04">
    <NodeName>defg</NodeName>
   </ExitPoint>
   <ExitPoint ID="22">
    <NodeName>mnop</NodeName>
   </ExitPoint>
  </SkillMap>

I am trying to get the value of the <ExitPoint> node based on the ID value i.e. if i enter the ID as 01 it should give "abcd" and if 22 it should give "mnop" and so on, but i'm not getting it, tried lot, please help.

Thanks, Ars

2
  • You should you use Xpath for that. Commented Jun 8, 2012 at 17:13
  • what you have tried so far? have you faced any issue? Commented Jun 8, 2012 at 17:17

1 Answer 1

1

You can do it using Xpath, consider the following example taken from the JAXP Specification 1.4 (which I recommend you to consult for this):

// parse the XML as a W3C Document
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.Document document = builder.parse(new File("/widgets.xml"));
// evaluate the XPath expression against the Document
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/widgets/widget[@name='a']/@quantity";
Double quantity = (Double) xpath.evaluate(expression, document, XPathConstants.NUMBER);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks but for the above xml whether i've to use String expression = "/ExitPoint/widget[@name='ID']/@quantity"; or wat?
I think that if you read the material I have shared in my answer, you will eventually be capable of figuring out yourself. And I think that's the way it should be.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.