Code Robo
Formatter
Comparator
Tester
Converter
Utility
Java Code Complience
Validator
EncoderDecoder
Virtual Service
How To Parse XML File / How to read data from XML File in Java
       Talk to EasyAssistant

This page demonstrate how to read data from XML file using DOM Parser. . There is a simple one.

Here one example is given on how to retrieve/read data from a XML file or XML Document. In this example we will be using following XML. It has four levels of tag (Author>Books>Book>Price).
XML File Content:
<?xml version="1.0" encoding="UTF-8"?>
<Author DOB="" EMailID="" SSN="">
	<Extn ExternalSystemCode=""/>
	<Books>
		<Book BookNo="1" Name="Compiler" PublisherCode="DVT">
			<Extn ExtnGiftCoverCode=""/>
			<Price DiscountPercentage="" ListPrice="490" UnitOfMeasure=""/>
		</Book>
		<Book BookNo="2" Name="Microprocessor" PublisherCode="DFS">
			<Extn ExtnGiftCoverCode=""/>
			<Price DiscountPercentage="10" ListPrice="600" UnitOfMeasure="Each"/>
		</Book>
		<Book BookNo="3" Name="Compiler" PublisherCode="DVT">
			<Extn ExtnGiftCoverCode=""/>
			<Price DiscountPercentage="" ListPrice="490" UnitOfMeasure="">  Some VAlue</Price>
		</Book>
	</Books>
</Author>

Here We will show how to retrieve the ListPrice value for the Book with BookNo=2.
Java Code:
package com.easy.xmlparsing;

import java.io.File;
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.NodeList;
import org.xml.sax.SAXException;

public class ReadDataFromXml {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {

		DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		//Get Document Object from file.
		Document document = docBuilder.parse(new File("Repo.xml"));


		//Start with root element. Then navigate the desired / traget element.
		Element rootElement = document.getDocumentElement();
		
		//getElementsByTagName() method return List of Nodes (here element).
		//In the XML if that element appear only once, this node list will have only one element.
		NodeList nodeListBooks = rootElement.getElementsByTagName("Books");
		Element booksElm = (Element) nodeListBooks.item(0);

		NodeList nodeListBook = booksElm.getElementsByTagName("Book");
		Element desiredBookElm = null;
		//Iterate over the node list to get the correct/desired element
		for (int i = 0; i < nodeListBook.getLength(); i++) {
			Element bookElm = (Element) nodeListBook.item(i);
			String bookNo=bookElm.getAttribute("BookNo");
			if (("2").equals(bookNo)) {
				desiredBookElm = bookElm;
			}
		}

		NodeList nodeListPrice = desiredBookElm.getElementsByTagName("Price");
		
        	//This PriceElement node is  our desire element from where we need to read the ListPrice.
		Element priceElm = (Element) nodeListPrice.item(0);
		//Reading the value of ListPrice attritbute.
		String listPrice = priceElm.getAttribute("ListPrice");
		//Redading element content just to show how to get the element content value.
		String elmContent=priceElm.getTextContent();


		//Printig them in the console to display the output of our java code.
		System.out.println("listPrice of BookNo 2 is" + listPrice);
		System.out.println("elmContent=" + elmContent);

	}

}




Please provide your feedback here
It has been explained in this video (https://youtu.be/bxkNb0GuELY). Please watch it.
 



Post Your Comment:
Name :
Email ( Optional) :
Comments / Suggestion (* Required) It is required: :
: