XML parsing using DOM parser in java
In java we have multiple ways to parse XML. We will see steps for XML parsing using DOM parser in java in this post.
What are DOM parsers?
DOM parsers are parsers that load the complete XML into memory and create a Tree structure of the XML. This can be used to traverse/iterate through the nodes of the XML.
When to use DOM parsers?
1. Since the DOM parses load the entire XML into memory, its betters to use DOM parsers when you know that your XML file is small in size. If the XML is biggers, we can have memory issues.
2. If you need to search your XML backward/forward, use DOM parser.
3. If you want to add/delete any nodes in your XML
Steps for XML parsing using DOM parser in java
1. Create Document Builder Factory and Use that factory to create a instance of DocumentBuilder
1 2 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); |
2. Get a File object of the XML that we need to parse
1 | new File("C:\\kscodes\\employee.xml") |
3. Use the DocumentBuilder and File object to create a Document
1 | Document document = builder.parse(new File("C:\\kscodes\\employee.xml")); |
4. Get a list of nodes that we need to parse/display
1 | NodeList nList = document.getElementsByTagName("employee"); |
5. We can now iterate the NodeList to get each node and element inside those node
1 | Node node = nList.item(i); |
Sample Code
1. XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <employeeDetails> <employee id="0001"> <name>John Doe</name> <department>Sales</department> <salary>$5000</salary> </employee> <employee id="0002"> <name>Scott Tiger</name> <department>HR</department> <salary>$4000</salary> </employee> <employee id="0003"> <name>David Smith</name> <department>Security</department> <salary>$4500</salary> </employee> </employeeDetails> |
2. Java Code
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 com.kscodes.sampleproject; 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 XmlDomParser { public static void main(String[] args) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("C:\\kscodes\\employee.xml")); document.getDocumentElement().normalize(); Element root = document.getDocumentElement(); System.out.println("The Root of the XML is :: " + root.getNodeName()); System.out.println("_____________________"); NodeList nList = document.getElementsByTagName("employee"); for (int i = 0; i < nList.getLength(); i++) { Node node = nList.item(i); System.out.println("Employee " + (i + 1)); if (node.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) node; System.out.println("ID : " + eElement.getAttribute("id")); System.out.println("Name : " + eElement.getElementsByTagName("name").item(0).getTextContent()); System.out.println("Department : " + eElement.getElementsByTagName("department").item(0).getTextContent()); System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent()); System.out.println("_____________________"); } } } catch (Exception e) { System.out.println("Exception occured" + e); } } } |
3. Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | The Root of the XML is :: employeeDetails _____________________ Employee 1 ID : 0001 Name : John Doe Department : Sales Salary : $5000 _____________________ Employee 2 ID : 0002 Name : Scott Tiger Department : HR Salary : $4000 _____________________ Employee 3 ID : 0003 Name : David Smith Department : Security Salary : $4500 _____________________ |