1

XML OUTPUT

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     <HelpListResponse>
         <HelpList>
    <id>16639505</id>
    <name>HelpList_Old</name>
    <details>HelpList/16639505</details>
    </HelpList>
    <HelpList>
    <id>16641505</id>
   <name>Investments to Watch</name>
    <details>HelpList/16641505</details>
    </HelpList>
    <HelpList>
    <id>16640505</id>
    <name>HelpList_New</name>
    <details>HelpList/16640505</details>
    </HelpList>
    </HelpListResponse>

JAVA CODE for XML Parsing...

 xmlFile = new File("xmlFile.xml");
   DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
   DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
   Document doc = dBuilder.parse(xmlFile);

I am parsing above xml document and storing it in doc object. For instance, if i need to retrieve name value from first index (ie:"HelpList_Old"), then i use the below statement

doc.getElementsByTagName("name").item(0).getTextContent();
 => This helps me to print "HelpList_Old"

I need couple of things and I am not sure how to go about it… 1) I need to get index value based on the text content of xml. (eg: - If I provide "HelpList_Old", then it should return me index = 0, similarly if i want to get the index of value "HelpList_New" it should return 2)

2) On same lines, if I provide "HelpList_Old", then I want to print its corresponding tag value, in this case “16639505”

Let me know in case I am not clear with my question. Thanks in Advance..

LearningSnippet
  • 101
  • 1
  • 1
  • 5
  • 1
    You should solve this with XPath. And I'm pretty sure you don't need the indexes - can you explain what you would use them for? Also, please include the Java code that creates the `doc` variable. – Tomalak May 03 '13 at 07:10
  • I am not sure how to use xpath approach with respect to above code. As far as getting index number is related, I am trying to move values from one index to another using some api function, so by getting index number, it'll be lesser steps for me to validate the response output. – LearningSnippet May 03 '13 at 16:56
  • Check out http://stackoverflow.com/questions/226405/find-position-of-a-node-using-xpath – Pace May 03 '13 at 21:47

0 Answers0