Read XML file using DOM
The DOM is a collection of interfaces that make it easy to access and modify the structure and content of an XML file. In this article, we will learn how to read XML files using your DOM!
DOM interfaces
The DOM defines several Java interfaces, below are some of the interfaces that we often use when working with it:
- Document
- Node
- Element
- Attr
- Text
I will explain each of these interfaces when we go into a specific example.
For example, reading XML files using DOM
In order for you to imagine, I will create and work with a Maven project to illustrate this article.
I have the following project:
The XML file we need to read is students.xml with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | XHTML <? xml version = "1.0" encoding = "UTF-8" standalone = "no"?> <students> <student n0 = '1'> <name> John </name> <code> 12345 </code> <age> 19 </age> </student> <student n0 = '2'> <name> Marry </name> <code> 23456 </code> <age> 24 </age> </student> </students> |
This file contains information for 2 students including name ( name ), student code ( code ) and age ( age ) of each student. Each student will be numbered through the n0 attribute .
OK, let's start reading this file!
First, to read the students.xml file, we need to initialize the File object for this file:
1 | File f = new File ("students.xml"); |
Next, we need to initialize the Document object containing the information of the XML file from the File object created above
1 2 3 4 | Java DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder buider = factory.newDocumentBuilder (); Document doc = buider.parse (f); |
To read the XML file with the DOM, we will read each of its tags from outside into in their hierarchical order.
In students.xml file, the student tag is the most outer tag, or root element. To read this tag we use the following method:
1 | Element students = doc.getDocumentElement (); |
In the DOM, a tag is defined as an Element.
Now that we have student card information, to get student card information, we will use student cards to get them.
1 | NodeList studentList = students.getElementsByTagName ("student"); |
The NodeList object will contain the student 2 tag information contained in the XML file. Now we will go through each card to read their information:
1 2 3 4 5 6 7 8 | Java for (int i = 0; i <studentList.getLength (); i ++) { Node node = studentList.item (i); if (node.getNodeType () == Node.ELEMENT_NODE) { Element student = (Element) node; } } |
A Node is a node in our DOM tree. We need to return it to the Element object to use.
OK, now we have the object containing the student information. The task now is to read them.
To read the sequence number attribute, we just need to do the following:
1 | student.getAttribute ("n0"); |
with n0 is the name of the attribute.
Because we are currently reading the student card and each student card has only one child card for name, student code and age, we can read this information as follows:
Name tags:
1 | student.getElementsByTagName ("name"). item (0) .getTextContent (); |
Student code card:
1 | student.getElementsByTagName ("code"). item (0) .getTextContent (); |
Age card:
1 | student.getElementsByTagName ("age"). item (0) .getTextContent (); |
OK, we have finished reading students.xml file, you can refer to the full code as follows:
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 | Java package com.huongdanjava; import java.io.File; import java.io.IOException; 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; import org.xml.sax.SAXException; public class DomExample { public static void main (String [] args) throws ParserConfigurationException, SAXException, IOException { File f = new File ("students.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder buider = factory.newDocumentBuilder (); Document doc = buider.parse (f); Element students = doc.getDocumentElement (); NodeList studentList = students.getElementsByTagName ("student"); for (int i = 0; i <studentList.getLength (); i ++) { Node node = studentList.item (i); if (node.getNodeType () == Node.ELEMENT_NODE) { Element student = (Element) node; System.out.println ("n0:" + student.getAttribute ("n0")); System.out.println ("name:" + student.getElementsByTagName ("name"). Item (0) .getTextContent ()); System.out.println ("code:" + student.getElementsByTagName ("code"). Item (0) .getTextContent ()); System.out.println ("age:" + student.getElementsByTagName ("age"). Item (0) .getTextContent ()); System.out.println ("n"); } } } } |
Techtalk talk via huongdanjava