How to read an XML file using XPath in Java?

0 votes
Is there any simple way to read an XML file using XPath in Java?
Jun 14, 2018 in Java by Annie
• 240 points
696 views

1 answer to this question.

0 votes

Import the packages required to work with XPath:

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
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.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

Then create instances of DocumentBuilderFactory, DocumentBuilder and document:

public class XPathReaderDemo {


public static void main(String[] args) {


try {

         File inputFile = new File("D:\\students.txt");

         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

         DocumentBuilder dBuilder;


         dBuilder = dbFactory.newDocumentBuilder();


         Document doc = dBuilder.parse(inputFile);

         doc.getDocumentElement().normalize();


         XPath xPath =  XPathFactory.newInstance().newXPath();


         String expression = "/class/student";       

         NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(

            doc, XPathConstants.NODESET);

for (int i = 0; i < nodeList.getLength(); i++) {

            Node nNode = nodeList.item(i);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

           

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

               Element eElement = (Element) nNode;

              

               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("email : "

                  + eElement

                  .getElementsByTagName("email")

                  .item(0)

                  .getTextContent());

               System.out.println("Marks : "

                  + eElement

                  .getElementsByTagName("marks")

                  .item(0)

                  .getTextContent());

            }

         }

 } catch (ParserConfigurationException e) {

         e.printStackTrace();

      } catch (SAXException e) {

         e.printStackTrace();

      } catch (IOException e) {

         e.printStackTrace();

      } catch (XPathExpressionException e) {

         e.printStackTrace();

      }


  }


}
answered Jun 14, 2018 by Akrati
• 3,150 points

Related Questions In Java

0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
90 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 8,230 views
0 votes
2 answers

How can I create File and write data in it using Java?

import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class WriteFiles{ ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
91 views
0 votes
3 answers

How to sort an array in java?

import java.util.Arrays; public class Sort { ...READ MORE

answered Aug 24, 2018 in Java by parth295
• 4,620 points
62 views
0 votes
1 answer

How to pretty print XML from Java?

Transformer transformer = TransformerFactory.newInstance().newTransf ...READ MORE

answered Jul 4, 2018 in Java by Akrati
• 3,150 points
121 views
0 votes
1 answer

org.xml.sax.SAXParseException: How to handle this exception?

This is often caused by a white ...READ MORE

answered Jul 9, 2018 in Java by sharth
• 3,320 points
692 views
0 votes
1 answer

Hosting spring boot app

There are a couple of things wrong ...READ MORE

answered Sep 11, 2018 in AWS by Priyaj
• 56,540 points
274 views
0 votes
1 answer

web.xml file is missing in my eclipse IDE

Very simple. You will find Deployment Descriptor ...READ MORE

answered Nov 27, 2018 in Java by Omkar
• 67,480 points
73 views
0 votes
2 answers

How to read a text file in Java?

You can use Scanner class to read ...READ MORE

answered Aug 9, 2018 in Java by parth295
• 4,620 points
49 views
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System.o ...READ MORE

answered Aug 16 in Java by Sirajul
• 25,940 points
208 views