how to get XML node value

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

how to get XML node value

Poornima hebbar
 <Rates room="1">
        <OccupantDetails>
          <Adult count="1"/>
          <Senior count="0"/>
          <Children count="0"/>
        </OccupantDetails>
      </Rates>
      <Content>
        <CheckInTime>1600</CheckInTime>
        <CheckOutTime>1100</CheckOutTime>

tarun please help me in getting adult count
Reply | Threaded
Open this post in threaded view
|

Re: how to get XML node value

softwaretestingforum
Administrator
This works for me -

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLParsing {
       
        public static void main(String[] arg) throws ParserConfigurationException, SAXException, IOException {
                File file = new File("/home/tarunb/Desktop/adultcount.xml");
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                Document document = documentBuilder.parse(file);
                NodeList nodeList = document.getElementsByTagName("Adult");
                System.out.println(nodeList.item(0).getAttributes().getNamedItem("count").getTextContent());
        }
}
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: how to get XML node value

Poornima hebbar
Its working .. thanks tarun :)
Reply | Threaded
Open this post in threaded view
|

Re: how to get XML node value

Muaaz
Hi Tarun ,

Assume my XML name is changing , by adding a current date and version name ,

then how do we do that ,

Thank you ,
 
Reply | Threaded
Open this post in threaded view
|

Re: how to get XML node value

softwaretestingforum
Administrator
Could you post a sample xml then I might be able to help better.
~ seleniumtests.com