Manual and Automated Testing
›
General Discussion
Search
everywhere
only in this topic
Advanced Search
how to get XML node value
Classic
List
Threaded
♦
♦
Locked
5 messages
Poornima hebbar
Reply
|
Threaded
Open this post in threaded view
♦
♦
|
how to get XML node value
<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
softwaretestingforum
Reply
|
Threaded
Open this post in threaded view
♦
♦
|
Re: how to get XML node value
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
Poornima hebbar
Reply
|
Threaded
Open this post in threaded view
♦
♦
|
Re: how to get XML node value
Its working .. thanks tarun :)
Muaaz
Reply
|
Threaded
Open this post in threaded view
♦
♦
|
Re: how to get XML node value
Hi Tarun ,
Assume my XML name is changing , by adding a current date and version name ,
then how do we do that ,
Thank you ,
softwaretestingforum
Reply
|
Threaded
Open this post in threaded view
♦
♦
|
Re: how to get XML node value
Administrator
Could you post a sample xml then I might be able to help better.
~ seleniumtests.com
Free forum by Nabble
Edit this page