DOM

Parse XML file with DOM

This is an example of how to parse an xml file using a DOM Document. The DOM Document interface represents the entire HTML or XML document and provides the primary access to a document’s data. Parsing an xml file using a DOM Document implies that you should:

  • Obtain a new instance of a DocumentBuilderFactory, that is a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents.
  • Set the parser produced so as not to validate documents as they are parsed, using setValidating(boolean validating) API method of DocumentBuilderFactory, with validating set to false.
  • Create a new instance of a DocumentBuilder, using newDocumentBuilder() API method of DocumentBuilderFactory.
  • Parse the FileInputStream with the content to be parsed, using parse(InputStream is) API method of DocumentBuilder. This method parses the content of the given InputStream as an XML document and returns a new DOM Document object.
  • Get the NodeList of all the Element objects in the document order with a given tag name, using
    getElementsByTagName(String tagname) API method of Document. You can get the first element in the nodeList, and for this node, you can get its first child, using getFirstChild() API method of Node, and then its value, using getNodeValue() API method of Node.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.io.File;
import java.io.FileInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ParseXMLFileWithDOM {
	
	public static void main(String[] args) throws Exception {
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setValidating(false);
    	DocumentBuilder db = dbf.newDocumentBuilder();
    	Document doc = db.parse(new FileInputStream(new File("in.xml")));
    	
    	Element channelNode = (Element) doc.getElementsByTagName("channel").item(0);
    	
    	Node titleNode = channelNode.getElementsByTagName("title").item(0);
    	String title = titleNode.getFirstChild().getNodeValue();
    	System.out.println("Title: " + title);

    	Node languageNode = channelNode.getElementsByTagName("language").item(0);
    	String language = languageNode.getFirstChild().getNodeValue();
    	System.out.println("Language: " + language);
    	
    	NodeList itemsList = doc.getElementsByTagName("item");
    	
    	int itemsCount = itemsList.getLength();
    	
    	for (int i = 0; i<itemsCount; i++) {	
    		
    		System.out.println("Item: ");
			
    		Node itemNode = itemsList.item(i);
			Element item = (Element) itemNode;
			
			Node itemTitleNode = item.getElementsByTagName("title").item(0);
			String itemTitle = itemTitleNode.getFirstChild().getNodeValue();
	    	System.out.println("tTitle: " + itemTitle);			
			
			Node linkNode = item.getElementsByTagName("link").item(0);
			String link = linkNode.getFirstChild().getNodeValue();
	    	System.out.println("tLink: " + link);		
			
			Node pubDateNode = item.getElementsByTagName("pubDate").item(0);
			String pubDate = pubDateNode.getFirstChild().getNodeValue();
	    	System.out.println("tPubDate: " + pubDate);
			
		}
		
	}

}

Input:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title>Java Tutorials and Examples</title>
		<language>en-us</language>
		<item>
			<title><![CDATA[Java Tutorials]]></title>
			<link>http://www.javacodegeeks.com/</link>
			<pubDate>Sun, 16 Nov 2008 23:25:02 -0600</pubDate>
		</item>
		<item>
			<title><![CDATA[Java Examples]]></title>
			<link>http://examples.javacodegeeks.com/</link>
			<pubDate>Sun, 16 Nov 2008 23:25:02 -0600</pubDate>
		</item>
	</channel>
</rss>

Output:

Title: Java Tutorials and Examples
Language: en-us
Item: 
	Title: Java Tutorials
	Link: http://www.javacodegeeks.com/
	PubDate: Sun, 16 Nov 2008 23:25:02 -0600
Item: 
	Title: Java Examples
	Link: http://examples.javacodegeeks.com/
	PubDate: Sun, 16 Nov 2008 23:25:02 -0600

 
This was an example of how to parse an xml file using a DOM Document in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button