This article describes the ways to read XML file in Java and get XML node values by providing XML Node names and by giving XPath for the specific XML node element.While automating an application ( using Selenium or LeaFT or Web Services Automation ) at times we need to read test data from a XML file. On the off chance that you have a similar requirement, this article will facilitate your work.
We will use the following sample XML file to read its values.
Read XML File in Java and Get XML Node Values by Providing Node Name
The following example shows how to read XML file in Java and print all node values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package mypack; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class ReadXMLFileExample { public static void main(String[] args) { try { File file = new File("C:\\SampleXML.xml"); //Create a new object of DocumentBuilderFactory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //Create an object DocumentBuilder to parse the XML file data DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); doc.getDocumentElement().normalize(); System.out.println("Root element name is: " + doc.getDocumentElement().getNodeName()); //Creating a list of nodes present in the XML file NodeList nodeList = doc.getElementsByTagName("mobile"); for (int i = 0; i< nodeList.getLength(); i++) { Node node = nodeList.item(i); System.out.println("\n"+"("+i+")" +" Child Node Name :" + node.getNodeName()); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; System.out.println("Mobile id: "+ element.getElementsByTagName("id").item(0).getTextContent()); System.out.println("Brand Name: "+ element.getElementsByTagName("brand").item(0).getTextContent()); System.out.println("Model Name: "+ element.getElementsByTagName("model").item(0).getTextContent()); System.out.println("Mobile Price: "+ element.getElementsByTagName("price").item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } } } |
Output
Root element name is: item
(0) Child Node Name : mobile
Mobile id: 101
Brand Name: Samsung
Model Name: Samsung Galaxy F62
Mobile Price: 320$
(1) Child Node Name : mobile
Mobile id: 102
Brand Name: Motorola
Model Name: Motorola G60
Mobile Price: 800$
(2) Child Node Name : mobile
Mobile id: 103
Brand Name: Apple
Model Name: Apple iPhone12
Mobile Price: 1050$
(3) Child Node Name : mobile
Mobile id: 104
Brand Name: Oneplus
Model Name: Oneplus Nord CE 5G
Mobile Price: 305$
Read XML File in Java and Get XML Node Values by Providing XPath
We will read the value of brand tag for the second mobile item.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package mypack; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; public class ReadXMLFileNodeByXPath { public static void main(String[] args) { try { File file = new File("C:\\SampleXML.xml"); //Create a new object of DocumentBuilderFactory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //Create an object DocumentBuilder to parse the XML file data DocumentBuilder db = dbf.newDocumentBuilder(); Document xmlDoc = db.parse(file); xmlDoc.getDocumentElement().normalize(); // Motorola mobile is second child of mobile tag String strBrandName = getNodeValueFromDom(xmlDoc,"//mobile[position()=2]/brand"); System.out.println("Brand Name is: " + strBrandName); } catch (Exception e) { e.printStackTrace(); } } //Method to get XML Node value using XPath public static String getNodeValueFromDom(Document xmlDoc, String strXPath) { try { XPath xPath = XPathFactory.newInstance().newXPath(); return xPath.compile(strXPath).evaluate(xmlDoc); } catch (XPathExpressionException e) { e.printStackTrace(); } return null; } } |
Output
Brand Name is: Motorola
Conclusion
We have learned two different ways to read the value from an XML file in Java. You can use either of them as per your requirement. Hope you will find this article helpful. Please don’t forget to share this article with your friends.