-1

Hi I have one XML file which contains following form of tags

<values>
   <value>Yes</value>
   <value>No</value>
   <value>other</value>
</values>

what I need to do with is, I need iterate over these tags and get the values. I am using DOM parser technique to parse the XML file. How do I do this.

Note : <value> tag can increase in number. According to that, with the help of Java code I would be able to get all values from <value> tag .

This is what I did.

NodeList nodeList = doc.getElementsByTagName("values");
for (int temp1 = 0; temp1 < nodeList.getLength(); temp1++) 
{
    Node node = nodeList.item(temp1);

    if(node.getNodeType() == Node.ELEMENT_NODE)
    {
         Element element = (Element)node;
         String value = element.getElementsByTagName("value").item(0).getTextContent();
         System.out.println("Values are:::" + value);
    }

}
Roshan Yengul
  • 21
  • 1
  • 7

1 Answers1

2

I found the solution. Please refer updated below code:

NodeList nodeList = doc.getElementsByTagName("values");

for (int temp1 = 0; temp1 < nodeList.getLength(); temp1++) 
{
     Node node = nodeList.item(temp1);

     if(node.getNodeType() == Node.ELEMENT_NODE)
     {

      String value = node.getTextContent();

      System.out.println("value:::"+value);

     }

}

This will give you the output as:

Yes No Other

Roshan Yengul
  • 21
  • 1
  • 7