Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Read XML File in Java

In Java, using DocumentBuilderFactory, DocumentBuilder,XpathFactory and Xpath, you can read a xml document. In this example below, we read the "Name" tag under the "Test" node with attribute id. See the example below. 

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<TestCases>
  <Testid="1">
    <Name>Login</Name>
    <TestModule>Login Dashboard</TestModule>
    <TestType>Smoke Test</TestType>
    <TestExecution>Manual</TestExecution>
  </Test>
</TestCases>

ReadXmlFile.java (write the code below)

package com.example.xml;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class ReadXmlFile {
    public static void main(String[] args)
    {
        System.out.println(ReadFile("TestCases.xml","1"));
    }

            public static String ReadFile(String fileName,String id)
        {

                String testinput = null;
                String projectPath=System.getProperty("user.dir");
                System.out.println(projectPath);
                File fXmlFile = new File(projectPath+File.separator+fileName);
                System.out.println(fXmlFile);
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = null;
            try {
                dBuilder = dbFactory.newDocumentBuilder();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            }
            Document doc= null;
            try {
                doc = dBuilder.parse(fXmlFile);
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
                XPath xPath =  XPathFactory.newInstance().newXPath();
                String expression = String.format("/TestCases/Test[@id='"+id+"']/Name");
                System.out.println(expression);
            Node node = null;
            try {
                node = (Node) xPath.compile(expression).evaluate(doc, XPathConstants.NODE);
            } catch (XPathExpressionException e) {
                e.printStackTrace();
            }
            testinput = node != null ? (node.getTextContent()) : "cannot read the test case xml file";
                return testinput;

        }
    }


Run the code and see the result. The text in the xpath "/TestCases/Test[@id="1"]/Name is printed out.

Root element :TestCases
/TestCases/Test[@id='1']/Name
Login