How to read XML file in Java – (DOM Parser)

In this tutorial, we will show you how to read an XML file via DOM XML parser. DOM parser parses the entire XML document and loads it into memory; then models it in a “TREE” structure for easy traversal or manipulation.

In short, it turns a XML file into DOM or Tree structure, and you have to traverse a node by node to get what you want.

What is Node?
In the DOM, everything in an XML document is a node, read this.
Warning
DOM Parser is slow and consumes a lot of memory when it loads an XML document which contains a lot of data. Please consider SAX parser as solution for it, SAX is faster than DOM and use less memory.

1. DOM XML Parser Example

This example shows you how to get the node by “name”, and display the value.

/Users/mkyong/staff.xml

<?xml version="1.0"?>
<company>
	<staff id="1001">
		<firstname>yong</firstname>
		<lastname>mook kim</lastname>
		<nickname>mkyong</nickname>
		<salary>100000</salary>
	</staff>
	<staff id="2001">
		<firstname>low</firstname>
		<lastname>yin fong</lastname>
		<nickname>fong fong</nickname>
		<salary>200000</salary>
	</staff>
</company>
ReadXMLFile.java

package com.mkyong.seo;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXMLFile {

  public static void main(String argv[]) {

    try {

	File fXmlFile = new File("/Users/mkyong/staff.xml");
	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
	Document doc = dBuilder.parse(fXmlFile);
			
	//optional, but recommended
	//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
	doc.getDocumentElement().normalize();

	System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
			
	NodeList nList = doc.getElementsByTagName("staff");
			
	System.out.println("----------------------------");

	for (int temp = 0; temp < nList.getLength(); temp++) {

		Node nNode = nList.item(temp);
				
		System.out.println("\nCurrent Element :" + nNode.getNodeName());
				
		if (nNode.getNodeType() == Node.ELEMENT_NODE) {

			Element eElement = (Element) nNode;

			System.out.println("Staff id : " + eElement.getAttribute("id"));
			System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
			System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
			System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
			System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

		}
	}
    } catch (Exception e) {
	e.printStackTrace();
    }
  }

}

Result


Root element :company
----------------------------

Current Element :staff
Staff id : 1001
First Name : yong
Last Name : mook kim
Nick Name : mkyong
Salary : 100000

Current Element :staff
Staff id : 2001
First Name : low
Last Name : yin fong
Nick Name : fong fong
Salary : 200000

2. Looping the Node

This example reads the same "staff.xml", and showing you how to loop the node one by one, and print out the node name and value, and also the attribute if any.

ReadXMLFile2.java

package com.mkyong.seo;

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 ReadXMLFile2 {

  public static void main(String[] args) {

    try {

	File file = new File("/Users/mkyong/staff.xml");

	DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
                             .newDocumentBuilder();

	Document doc = dBuilder.parse(file);

	System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

	if (doc.hasChildNodes()) {

		printNote(doc.getChildNodes());

	}

    } catch (Exception e) {
	System.out.println(e.getMessage());
    }

  }

  private static void printNote(NodeList nodeList) {

    for (int count = 0; count < nodeList.getLength(); count++) {

	Node tempNode = nodeList.item(count);

	// make sure it's element node.
	if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

		// get node name and value
		System.out.println("\nNode Name =" + tempNode.getNodeName() + " [OPEN]");
		System.out.println("Node Value =" + tempNode.getTextContent());

		if (tempNode.hasAttributes()) {

			// get attributes names and values
			NamedNodeMap nodeMap = tempNode.getAttributes();

			for (int i = 0; i < nodeMap.getLength(); i++) {

				Node node = nodeMap.item(i);
				System.out.println("attr name : " + node.getNodeName());
				System.out.println("attr value : " + node.getNodeValue());

			}

		}

		if (tempNode.hasChildNodes()) {

			// loop again if has child nodes
			printNote(tempNode.getChildNodes());

		}

		System.out.println("Node Name =" + tempNode.getNodeName() + " [CLOSE]");

	}

    }

  }

}

Result :


Root element :company

Node Name =company [OPEN]
Node Value =
	
		yong
		mook kim
		mkyong
		100000
	
	
		low
		yin fong
		fong fong
		200000
	


Node Name =staff [OPEN]
Node Value =
		yong
		mook kim
		mkyong
		100000
	
attr name : id
attr value : 1001

Node Name =firstname [OPEN]
Node Value =yong
Node Name =firstname [CLOSE]

Node Name =lastname [OPEN]
Node Value =mook kim
Node Name =lastname [CLOSE]

Node Name =nickname [OPEN]
Node Value =mkyong
Node Name =nickname [CLOSE]

Node Name =salary [OPEN]
Node Value =100000
Node Name =salary [CLOSE]
Node Name =staff [CLOSE]

Node Name =staff [OPEN]
Node Value =
		low
		yin fong
		fong fong
		200000
	
attr name : id
attr value : 2001

Node Name =firstname [OPEN]
Node Value =low
Node Name =firstname [CLOSE]

Node Name =lastname [OPEN]
Node Value =yin fong
Node Name =lastname [CLOSE]

Node Name =nickname [OPEN]
Node Value =fong fong
Node Name =nickname [CLOSE]

Node Name =salary [OPEN]
Node Value =200000
Node Name =salary [CLOSE]
Node Name =staff [CLOSE]
Node Name =company [CLOSE]
Note
You may interest at this How to get Alexa Ranking In Java. It shows you how to use DOM to parse the Alexa XML result.

References

  1. When to Use DOM
  2. Normalization in DOM parsing with java - how does it work?
  3. Learn XML DOM
  4. What is Node?
  5. What is Element?

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
152 Comment threads
44 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
145 Comment authors
xXxLa RanobodychaitanyaAndres Recent comment authors
newest oldest most voted
Serch
Guest
Serch

TE AMO!!!

chaitanya
Guest
chaitanya

Hi Experts,

I have XML data which contains valid ‘&'(‘&’) and invalid ‘&'(just ‘&’). How do i handle it?
for ex:
input :
“&test & test& &”
output:
“&test & test& &”

how do I do it in java?

chaitanya
Guest
chaitanya

I am sorry. The ‘amp’ is not showing in the query. :(

Andres
Guest
Andres

public void leerXMLDom() throws ParserConfigurationException, SAXException, IOException, TransformerException {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
File fichero = new File(“nuevo.xml”);
Document doc = builder.parse(fichero);
Node raiz = doc.getFirstChild();
this.procesarXML(raiz);
this.addNodoDiv(doc);
this.guardarDOMcomoFILE(doc);
}

private void procesarXML(Node nodo) {

NodeList nodosHijo = nodo.getChildNodes();
System.out.print(“<" + nodo.getNodeName());
NamedNodeMap atributos = nodo.getAttributes();
for (int i = 0; i n”);
String valorNodo = nodo.getFirstChild().getNodeValue();
if (valorNodo != null) {
System.out.println(valorNodo);
}
for (int i = 0; i 0) {
this.procesarXML(nodoHijo);
}
}
System.out.println(“”);
}

Suresh
Guest
Suresh

Hello,

I would like to know how we can print comments present in xml nodes.

kalyan
Guest
kalyan

God examples Mr.Mkyong but may i know what is DocumentBuilderFactory,DocumentBuilder. what is their role here

pandi
Guest
pandi

what is the use of normalize() please explain about that method?

oma
Guest
oma

How do I read the xml file from a URL

Richard Ellicott
Guest
Richard Ellicott

really helpful examples thanks

ksoileau
Guest
ksoileau

Thanks, your article was just what I needed…:)

MyNameIsAlwaysTaken
Guest
MyNameIsAlwaysTaken

Thank you for this example. It was very useful

frank
Guest
frank

Hello! i would like to know how is possible to write a test class with Junit from your example DOM XML Parser, can you give us an example? thanks

AutoD
Guest
AutoD

i have used this similar code and tried to run the code, but its not allowing me to run it as java application instead its just showing run configuration

Rakesh Sonu
Guest
Rakesh Sonu

boss getTextContent() is throwning an error .May i know what i need to do.

Girish Kumar ch
Guest
Girish Kumar ch

Getting following error while executing the code Exception in thread “main” java.lang.NoSuchFieldError: fFeatures at org.apache.xerces.parsers.XML11Configuration.(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.(Unknown Source) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at java.lang.Class.newInstance0(Class.java:372) at java.lang.Class.newInstance(Class.java:325) at org.apache.xerces.parsers.ObjectFactory.newInstance(Unknown Source) at org.apache.xerces.parsers.ObjectFactory.findJarServiceProvider(Unknown Source) at org.apache.xerces.parsers.ObjectFactory.createObject(Unknown Source) at org.apache.xerces.parsers.ObjectFactory.createObject(Unknown Source) at org.apache.xerces.parsers.DOMParser.(Unknown Source) at org.apache.xerces.parsers.DOMParser.(Unknown Source) at org.apache.xerces.jaxp.DocumentBuilderImpl.(Unknown Source) at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.newDocumentBuilder(Unknown Source) at ReadXMLFile.main(ReadXMLFile.java:19)

Nhlanhla Styls Mazibuko
Guest
Nhlanhla Styls Mazibuko

Mkyong…

How do i get the jars for the:

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document
Regards
Nhlanhla

Yung
Guest
Yung

Seriously. Thank you dude. T_T

Vladimir Voronitski
Guest
Vladimir Voronitski

great ,thnk u

avnish alok
Guest
avnish alok

CS HOT TOPICS is a technical blog for open source enthusiasts that always updates with latest tools & technologies. As the name
suggest it covers hot topics of computer programming in various
languages(java,jsp,servlet,php) to name a few with latest
tools(ajax,jquery). You can get more to visit here http://cshotopics.blogspot.com/

riki bahal
Guest
riki bahal

good one..

Sarah
Guest
Sarah

Thank you, you really helped me :)

tiara
Guest
tiara

why you didnt print the attribute? Im very curious, thanks

Jahed Hossain
Guest
Jahed Hossain

How do I loop through a deep XML file such as this:

NKumar
Guest
NKumar

for looping the node , I want to print the output in a file with UTF-8 format. I have similar requirement but need to print in the file. The input xml contain thai character. Please help what code should be modified to print it in a file with UTF-8.

Necmp
Guest
Necmp

For the looping the node. I want to write into the file but I am not able to write in UTF-8 format. Can anyone please help me on this? How the code should change. I am new to java and really need this.

Ando
Guest
Ando

Thank you very much :)

Giovanni Luis Barrantes Lazo
Guest
Giovanni Luis Barrantes Lazo

Thank You.

ankur saxena
Guest
ankur saxena

bhai attribute na mail ra

Nikhil
Guest
Nikhil

I want to merge the some element from one xml to another using dom parser

source xml is

dest xml is

I want to add element who’s display name is “Master Data” in between the element of other xml using dom parser.

trackback
Parsing XML in Java | Stackforum.com

[…] I have got this XML file which is not well formatted but I need to to parse this anyhow.I have tried all parsing options say DOM / SAX parsing but still could not achieve it, Tried this […]

trackback
Como ler arquivos XML com Java – (SAX Parser) | Casa Do Desenvolvedor

[…]   Note Você pode se interessar por: Como ler arquivos XML com Java – (SAX Parser) […]

MANIKANDAN
Guest
MANIKANDAN

below code creating a output file after parsing a xml file package domparsing; import org.w3c.dom.Document; import org.xml.sax.SAXParseException; import org.xml.sax.ext.DefaultHandler2; class MyHandler extends DefaultHandler2{ public MyHandler() { } public void warning(SAXParseException ex){ ex.getLineNumber(); ex.getColumnNumber(); } public void error(SAXParseException ex){ ex.getLineNumber(); ex.getColumnNumber(); } public void fatalError(SAXParseException ex){ ex.getLineNumber(); ex.getColumnNumber(); } } import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class ReadingDocument { public static void main (String[] arg)throws Exception { try{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); factory.isNamespaceAware(true); factory.isValidating(true); DocumentBuilder docBuilder = factory.newDocumentBuilder(); MyHandler mh = new MyHandler(); docBuilder.setErrorHandler(mh); Document doc = docBuilder.parse(“Sample.xml”); } catch(Exception ex){ ex.printStackTrace(); } } } xml part: Jane Smith AT&T (212) 555-4567

sri
Guest
sri

thanq so much but i want display the values into grid by using zul in eclipse