Traverse XML file using DOM Parser of JAXP

Check this post to get the idea about how to create XML document using DOM Parser and XSLT API.

In DOM Parser of JAXP, textContent() method of node does not give the text for that node. We must typecast the node to Element and then try this method.

See below code for implementation:

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" />

</Movies>

Java Code:

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 ReadXML {
	public static void main(String[] args) {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		try {
			builder = factory.newDocumentBuilder();
			Document doc = builder.parse("ShivaSoft.xml");

			//Get Root Node and its child
			Node root = doc.getDocumentElement();

			NodeList childNodes = root.getChildNodes();
			for(int i=0;i<childNodes.getLength();i++)
			{
				printNodeInfo(childNodes.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";
	  }
}

This entry was posted in JAVA and tagged on by .

About Jitendra

Sr. Consultant at Cognizant Technology Solutions. Have more than 6 years of experience in technologies and frameworks like Salesforce, Node.Js, JAVA, PHP, JSP, Struts, Hibernate, C#, ASP.NET, SQL Server. Also many javaScript libraries like AngularJs, JQuery, JQuery Mobile, BootStrap. Connect : Linked In