Searching XML File using DOM Parser of JAXP

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with Leave a comment on Searching XML File using DOM Parser of JAXP

Example of 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"?>
	<Movie Actor="Aamir Khan" Name="Lagaan" Type="BollyWood">
		This is node description for Movie Lagaan
	<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" />
		Yamla Pagala Deewana

Java Sourcecode:

package com.G2.DOM;


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++)
		} catch (ParserConfigurationException e) {
		} catch (SAXException e) {
		} catch (IOException e) {

	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;
			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()));
	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";


Related posts

Leave a Reply

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