How to read XML file in Java – (DOM Parser)
Written on December 6, 2008 at 2:46 pm by
mkyong
Here’s an example to demonstrate how to read a XML file in Java with DOM parser. The DOM interface is the easiest to understand. It parses an entire XML document and load it into memory, modeling it with Object for the traversal.
Note
DOM Parser is slow and consume a lot memory if it load a 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.
If you want to compare different implementation between DOM and SAX, Please visit this article – How to read XML file in Java – (SAX Parser)
Let’s start to see a DOM parser example.
<?xml version="1.0"?> <company> <staff> <firstname>yong</firstname> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>100000</salary> </staff> <staff> <firstname>low</firstname> <lastname>yin fong</lastname> <nickname>fong fong</nickname> <salary>200000</salary> </staff> </company>
A Java class to read the above XML file.
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("c:\\file.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); 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); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("First Name : " + getTagValue("firstname",eElement)); System.out.println("Last Name : " + getTagValue("lastname",eElement)); System.out.println("Nick Name : " + getTagValue("nickname",eElement)); System.out.println("Salary : " + getTagValue("salary",eElement)); } } } catch (Exception e) { e.printStackTrace(); } } private static String getTagValue(String sTag, Element eElement){ NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes(); Node nValue = (Node) nlList.item(0); return nValue.getNodeValue(); } }
Result
Root element :company ----------------------- First Name : yong Last Name : mook kim Nick Name : mkyong Salary : 100000 First Name : low Last Name : yin fong Nick Name : fong fong Salary : 200000
This article was posted in Java category.
All Java Tutorials
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery
[...] Read XML file – (DOM Parser) Example to read a XML file with DOM parser. [...]
I d like to get a domme like this one ! More posts like this?
‘Visual Basic
Module Module1
Sub Main()
Dim doc = XDocument.Load(“file.xml”)
Console.WriteLine(“Root element :” & doc.Root.Name.ToString)
Console.WriteLine(“———————–”)
For Each staff In doc…
Console.WriteLine(“First Name : ” & staff..Value)
Console.WriteLine(“Last Name : ” & staff..Value)
Console.WriteLine(“Nick Name : ” & staff..Value)
Console.WriteLine(“Salary : ” & staff..Value)
Next
End Sub
End Module
I can just say thank you for this wonderful post!
[...] P.S You can compare the syntax with SAX and DOM parser in the following two examples – How to read XML file in Java – (SAX Parser) – How to read XML file in Java – (DOM Parser) [...]
You may want to look at vtd-xml as the state of the art in XML processing, consuming far less memory than DOM
http://vtd-xml.sf.net
You are my hero
Thanks a lot for this.
For simple XML files this is much better than SAX Parser which to say the least is a f****** pain in the a**.
Both SAX and DOM has their target audience
Thanks for the tip man!