How to read XML file in Java using DOM Parser?
Steps:
1) Parse XML file or Create an instance of Document interface present in org.w3c.dom.* package.
For Creating an instance of Document interface we need to use DocumentBuilderFactory, DocumentBuilder classes present in javax.xml.parsers.* package and File class present in java.io.* package.
File file = new File(XML_file_location);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
Figure 1 |
2) Get the root element of XML file. It returns Employees node.
String rootNode = document.getDocumentElement().getNodeName();
3) Get all the child nodes Employee of root node Employees. It returns the instance of NodeList interface present in org.w3c.dom.* package.
NodeList list = document.getElementsByTagName("Employee");
4) Now one by one examine each child node Employee and extract its data.
pom.xml
Employee.java
Download Project
Click here for more questions on DOM Parser.
String rootNode = document.getDocumentElement().getNodeName();
3) Get all the child nodes Employee of root node Employees. It returns the instance of NodeList interface present in org.w3c.dom.* package.
NodeList list = document.getElementsByTagName("Employee");
4) Now one by one examine each child node Employee and extract its data.
- From all the child nodes that we got in Step 3 take one child node. It returns instance of Node interface present in org.w3c.dom.* package.
- Check whether this node is Element node or not?
- If it is an Element node then typecast it and get its data (attributes and properties).
- Get attribute value as below.
- Get property value as below.
Software Required:
- Java
- Eclipse
- Maven
Note : I suggest to use latest version of all the software. If you need any help regarding these software installation please comment below I will help you.
Project Structure:
Java Code:
employee.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <Employees> <Employee id="1"> <name>Rohit</name> <gender>male</gender> <age>25</age> <role>Programmer</role> </Employee> <Employee id="2"> <name>Bhavna</name> <gender>female</gender> <age>20</age> <role>Student</role> </Employee> </Employees>
pom.xml
Employee.java
ReadXML.java
Output:
Figure 3 |
Download Project
Click here for more questions on DOM Parser.
References:
https://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilderFactory.html
https://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Document.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/NodeList.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Element.html
Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.
How to read XML file in Java using DOM Parser?
Reviewed by Rohit Agarwal
on
9/29/2017
Rating:
No comments:
Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.