Searching XML File using DOM Parser of JAXP

Until now, we have already seen two examples of DOM Parser provided by JAXP:

  1. Create XML file using DOM
  2. Traverse xml file

In this tutorial, we will learn how to search the XML file for particular node in DOM Parser

Input File:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Movies>
	<Movie Actor="Aamir Khan" Name="Lagaan" Type="BollyWood">
		This is node description for Movie Lagaan
	</Movie>
	<Movie Actor="Aamir Khan" Name="Andaaz Apana Apna" Type="BollyWood" />
	<Movie Actor="Salman Khan" Name="Dabang" Type="BollyWood" />
	<Movie Actor="Salman Khan" Name="Wanted" Type="BollyWood" />
	<Movie Actor="AKshay Kumar" Name="Mujhse Shadi karoge" Type="BollyWood" />
<Movies2011>
	<jan>
		Yamla Pagala Deewana
</jan>
</Movies2011>
</Movies>

Java Sourcecode:

package com.G2.DOM;

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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class SearchXML {

	public static void main(String[] args) {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		try {
			builder = factory.newDocumentBuilder();
			Document doc=builder.parse("ShivaSoft.xml");
			NodeList nodes = doc.getElementsByTagName("Movies2011");
			for(int i=0;i<nodes.getLength();i++)
			{
				printNodeInfo(nodes.item(i));
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	static void printNodeInfo(Node node)
	{
		System.out.println(" Name : " + node.getNodeName());
		System.out.println(" Type : "+ nodeType(node.getNodeType()));
		if(node.getNodeType() == Node.ELEMENT_NODE)
		{
			Element e = (Element)node;
			System.out.println(e.getTextContent().trim());
		}
		if(node.hasAttributes())
		{
			NamedNodeMap rootAtt = node.getAttributes();
			for(int i=0;i<rootAtt.getLength();i++)
			{
				System.out.println(" Name : "+rootAtt.item(i).getNodeName());
				System.out.println(" Value : "+rootAtt.item(i).getNodeValue());
				System.out.println(" Type : "+nodeType(rootAtt.item(i).getNodeType()));
				System.out.println("--------------------------");
			}
		}
	}
	static String nodeType(short type) {
	    switch(type) {
	      case Node.ELEMENT_NODE:                return "Element";
	      case Node.DOCUMENT_TYPE_NODE:          return "Document type";
	      case Node.ENTITY_NODE:                 return "Entity";
	      case Node.ENTITY_REFERENCE_NODE:       return "Entity reference";
	      case Node.NOTATION_NODE:               return "Notation";
	      case Node.TEXT_NODE:                   return "Text";
	      case Node.COMMENT_NODE:                return "Comment";
	      case Node.CDATA_SECTION_NODE:          return "CDATA Section";
	      case Node.ATTRIBUTE_NODE:              return "Attribute";
	      case Node.PROCESSING_INSTRUCTION_NODE: return "Attribute";
	    }
	    return "Unidentified";
	  }

}

Leave a comment

Your email address will not be published.

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