Reading an XML document in Java with DOM Parse seems complicated at first but is actually quite easy.
DOM reads the entire document into memory before processing. DOM has the advantage of being able to insert or delete nodes and also has the ability to traverse in any direction. In this simple tutorial, I will show you how to extract the desired information from an XML document and print it to the console.
We will start with an XML document:
<?xml version="1.0"?> <university> <student> <name id="S1">Steve Smith</name> <age>20</age> <hobby>Computers</hobby> <hobby>Basketball</hobby> </student> <student> <name id="S2">Sean Taylor</name> <age>18</age> <hobby>Music</hobby> <hobby>Football</hobby> </student> <student> <name id="S3">Adam White</name> <age>22</age> <hobby>Sailing</hobby> <hobby>Music</hobby> <hobby>Fishing</hobby> </student> </university>
This is a simple XML file containing some student information from a local university.
The first step in writing the code is to import all the Classes that we need:
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import java.io.*;
To start, we want to get all of the 'student' nodes into a NodeList instance. To do this we use the getElementbyTagName method from the Document Class and we supply the name of the tag as a parameter.
NodeList nodes = doc.getElementsByTagName("student");
As a result, we now have all the student nodes in a NodeList instance called 'nodes' We can now iterate through each node and return and print all required data. Each of our students has a name, age and hobbies. As you can see, hobbies can be more than one field.
Next, we create an Element object that will point to a particular student node. A student element has child nodes directly under it. In this case: name, age & hobbies. We will create seperate NodeList objects for name age and hobbies using the getElementByTagName method.
Here is the completed code:
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import java.io.*; public class DOMParseExample { public static void main(String[] args) { File file = new File("C:\\xmlFile.xml"); try { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(file); NodeList nodes = doc.getElementsByTagName("student"); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); NodeList name = element.getElementsByTagName("name"); Element line = (Element) name.item(0); System.out.println("Student name: " + line.getFirstChild().getTextContent()); System.out.println("Id: " + line.getAttribute("id")); NodeList age = element.getElementsByTagName("age"); line = (Element) age.item(0); System.out.println("Age: " + line.getFirstChild().getTextContent()); NodeList hobby = element.getElementsByTagName("hobby"); for(int j=0;j<hobby.getLength();j++) { line = (Element) hobby.item(j); System.out.println("Hobby: " + line.getFirstChild().getTextContent()); } System.out.println(); } } catch (Exception e) { e.printStackTrace(); } } }
This output will be displayed in the console window:
Student name: Steve Smith Id: S1 Age: 20 Hobby: Computers Hobby: Basketball Student name: Sean Taylor Id: S2 Age: 18 Hobby: Music Hobby: Football Student name: Adam White Id: S3 Age: 22 Hobby: Sailing Hobby: Music Hobby: Fishing