How to read XML file in Java Using DOM Parser
In this article, we will learn how to read XML file in java using
DOM stands for
DOM parser parses the entire XML file, loads it into memory and then models it into a tree structure with each element representing tree branches.
Note:- DOM Parser is slow when XML document contains lot of data because it loads entire file into memory. Consider using SAX Parser when file size is high.
Note :- In Programs, consider changing
1. Java Program of DOM XML Parser
In this example we will get the node by “name” and display the value.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0"?> <contactbook> <employee id="101"> <name>Devil Donald</name> <email>donald.devil@abcd.com</email> <phone>+91 9090909090</phone> </employee> <employee id="201"> <name>Nevil Ray</name> <email>ray.nevil@abcd.com</email> <phone>+1 919191919</phone> </employee> </contactbook> |
0 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 |
package com.codenuclear; 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 ReadXMLUsingDOM { public static void main(String args[]) { try { File xmlFile = new File("D:\\Files\\contactbook.xml"); DocumentBuilderFactory docbuildFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docbuildFactory.newDocumentBuilder(); Document document = docBuilder.parse(xmlFile); document.getDocumentElement().normalize(); System.out.println("Root element name :- " + document.getDocumentElement().getNodeName()); NodeList nodeList = document.getElementsByTagName("employee"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); System.out.println("\nCurrent element name :- " + node.getNodeName()); System.out.println("-------------------------------------"); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; System.out.println("Employee id :- " + element.getAttribute("id")); System.out.println("Employee Name : " + element.getElementsByTagName("name").item(0).getTextContent()); System.out.println("Email: " + element.getElementsByTagName("email").item(0).getTextContent()); System.out.println("Phone: " + element.getElementsByTagName("phone").item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } } } |
Output
2. Java Program of DOM Parser with looping the nodes.
In this example we will loop through the nodes one by one and will display node name,values and attributes.
0 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
package com.codenuclear; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class ReadXMLByLoopingNode { public static void main(String[] args) { try { File xmlFile = new File("D:\\Files\\contactbook.xml"); DocumentBuilderFactory docbuildFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docbuildFactory.newDocumentBuilder(); Document document = docBuilder.parse(xmlFile); System.out.println("Root element name :- " + document.getDocumentElement().getNodeName()); if (document.hasChildNodes()) { printNode(document.getChildNodes()); } } catch (Exception e) { e.printStackTrace(); } } public static void printNode(NodeList nodeList) { for (int i = 0; i < nodeList.getLength(); i++) { Node tempNode = nodeList.item(i); // Ensure that the node is Element node if (tempNode.getNodeType() == Node.ELEMENT_NODE) { // print node name and value System.out.println("\nNode Name =" + tempNode.getNodeName()+" STARTS"); System.out.println("Node Value =" + tempNode.getTextContent()); // Check if attributes present in this node if (tempNode.hasAttributes()) { // get attributes names and values NamedNodeMap nodeMap = tempNode.getAttributes(); for (int j = 0; j < nodeMap.getLength(); j++) { Node node = nodeMap.item(j); System.out.println("Attribute Name : " + node.getNodeName()); System.out.println("Attribute Value : " + node.getNodeValue()); } } if (tempNode.hasChildNodes()) { // Check if child nodes present. printNode(tempNode.getChildNodes()); } System.out.println("Node Name :- " + tempNode.getNodeName()+" ENDS"); } } } } |
Output
Reference :- Reading XML