Read XML file in java using DOM parser
DOM stands for Document Object Model, XML DOM defines a standard for accessing and manipulating documents. DOM parser parses the entire XML document and creates DOM objects in the memory in form of a Tree structure. You should use DOM parser in following scenarios:
- In case of small XML documents, or large number of small XML documents.
- In case you want to access whole XML.
- For editing the xml as it keeps the DOM structure in memory.
1) Read XML file and print node names and values
XML file that you want to read: employees.xml
<?xml version="1.0"?> <employees> <employee id="123"> <firstname>Mohit</firstname> <lastname>Bisht</lastname> </employee> <employee id="124"> <firstname>Samit</firstname> <lastname>Ahlawat</lastname> </employee> <employee id="125"> <firstname>Vikram</firstname> <lastname>Raheja</lastname> </employee> </employees>
// Parses XML file using Java DOM parser and prints node names and values import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DOMXMLParser { public static void main(String[] args) { try { File inputFile = new File("C://employees.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = null; try { dBuilder = dbFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } Document doc = dBuilder.parse(inputFile); doc.getDocumentElement().normalize(); NodeList nodeList = doc.getElementsByTagName("employee"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); System.out.println("Current Element :" + node.getNodeName()); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; System.out.println("Employee id: " + element.getAttribute("id")); System.out.println("First Name : " + element.getElementsByTagName("firstname") .item(0).getTextContent()); System.out.println("Last Name : " + element.getElementsByTagName("lastname") .item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } } }
Output :
Current Element :employee Employee id: 123 First Name : Mohit Last Name : Bisht Current Element :employee Employee id: 124 First Name : Samit Last Name : Ahlawat Current Element :employee Employee id: 125 First Name : Vikram Last Name : Raheja
2) Read XML file and print node attribute values
XML file that you want to read: employees.xml
<?xml version="1.0"?> <employees> <employee id="123" department="accounts"> <firstname>Mohit</firstname> <lastname>Bisht</lastname> </employee> <employee id="124" department="hr"> <firstname>Samit</firstname> <lastname>Ahlawat</lastname> </employee> </employees>
// Parses XML file using Java DOM parser and prints attribute values import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.NamedNodeMap; public class DOMXMLParserReadAttributes { public static void main(String[] args) { try { File inputFile = new File("C://employees.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = null; try { dBuilder = dbFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } Document doc = dBuilder.parse(inputFile); doc.getDocumentElement().normalize(); NodeList nodeList = doc.getElementsByTagName("employee"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); System.out.println("Current Element :" + node.getNodeName()); if (node.getNodeType() == Node.ELEMENT_NODE) { NamedNodeMap nodeMap = node.getAttributes(); for (int j = 0; j < nodeMap.getLength(); j++) { Node node2 = nodeMap.item(j); System.out.println("attribute name : " + node2.getNodeName()); System.out.println("attribute value : " + node2.getNodeValue()); } } } } catch (Exception e) { e.printStackTrace(); } } }
Output :
Current Element :employee attribute name : department attribute value : accounts attribute name : id attribute value : 123 Current Element :employee attribute name : department attribute value : hr attribute name : id attribute value : 124
References:
https://www.w3schools.com/xml/dom_intro.asp
https://docs.oracle.com/javase/tutorial/jaxp/dom/readingXML.html