<?xml version="1.0" ?>
<firstname>saritha</firstname>
<lastname>chary</lastname>
<salary>10000</salary>
</emp>
<firstname>Babu</firstname>
<lastname>BhayaRaj</lastname>
<salary>1000000</salary>
</emp>
</listofname>
Java Program:
package SeleniumPrograms;
import java.io.File;
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.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class ReadforXmlfile {
public static void main(String[] args) throws Exception {
File fXmlFile = new File("E:\\sari\\os\\list.xml");//xml file path
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("emp");
System.out.println("_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode =nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());//it will display current element node
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("emp id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
}
}}
}
No comments:
Post a Comment