Getting text values from a NodeList

This Example shows you how to Get Text values from the NodeList in a DOM document.



Getting text values from a NodeList


This Example shows you how to Get Text values from the NodeList in a DOM document. JAXP (Java API for XML Processing) is an interface which provides parsing of xml documents. Here the Document BuilderFactory is used to create new DOM parsers.Some of the methods used in code given below are:-

File f = new File("Document3.xml"):-Creates a File From which Text is to be fetched.

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance():-This method Creates a DocumentBuilderFactory .DocumentBuilderFactory is a Class that enables application to obtain parser for building DOM trees from XML Document

DocumentBuilder builder = Factory.newDocumentBuilder():-This method creates a DocumentBuilder object with the help of a DocumentBuilderFactory.

Element root = doc.getDocumentElement():-This is a method by which we can have direct access to the root of the Document.

NodeList nodeList = doc.getElementsByTagName("Employee"):-This method returns a List of elements with the given tag name i.e-Employee.

NodeList fstNm = element1.getChildNodes():-By the use of this method we can get the collection of childnodes of the element1 node.

Xml code for the program generated is:-

<?xml version="1.0" encoding="UTF-8"?>
  <name>Girish Tewari
  <name>Mahendra Singh

 * @Program for getting text values from a NodeList
 * Author:-RoseIndia Team
 * Date:-17-July-2008

import org.w3c.dom.*;
import javax.xml.parsers.*;

public class GettingTextValues {
  public static void main(String[] args) throws Exception {
  File f = new File("Document3.xml");
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document doc = db.parse(f);
  new GettingTextValues().GettingText(doc);
  public void GettingText(Document doc) {
 Element root = doc.getDocumentElement();
  NodeList nodeList = doc.getElementsByTagName("Employee");
  for (int i = 0; i < nodeList.getLength(); i++) {
 Node node = nodeList.item(i);
 if (node.getNodeType() == Node.ELEMENT_NODE) {
  Element element = (Element) node;
  NodeList nodelist = element.getElementsByTagName("name");
  Element element1 = (Element) nodelist.item(0);
  NodeList fstNm = element1.getChildNodes();
  System.out.print("Name : " + (fstNm.item(0)).getNodeValue());
  //For retriving text from node City
  Element element2 = (Element) node;
  NodeList nodelist1 = element2.getElementsByTagName("City");
  Element element3 = (Element) nodelist1.item(0);
  NodeList fstNm1 = element3.getChildNodes();
  System.out.println("City : "+(fstNm1.item(0)).getNodeValue());
  //For retriving text from node Phoneno
  Element element4 = (Element) node;
  NodeList nodelist2 = 
  Element element5 = (Element) nodelist2.item(0);
  NodeList fstNm2 = element5.getChildNodes();
  + (fstNm2.item(

Output of the program:-

Name : Girish Tewari
  City : Haldwani
Name : Mahendra Singh
  City : Lucknow

Download Source Code



Share on Google+Share on Google+

Getting text values from a NodeList

Posted on: August 24, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Getting text values from a NodeList  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 27, 2011
xml parsing

Hi thanks for the very usefull post. and if there is <?xml version="1.0" encoding="UTF-8"?> <Company> <Division> <Employee> <name>Girish Tewari </name> <City>Haldwani</City>> <Phoneno>1234567890</Phoneno> </Employee> </Division <Division> <Employee> <name>Mahendra Singh </name> <City>Lucknow</City> <Phoneno>123652314</Phoneno>` </Employee> </division> </Company>
February 21, 2014
Error in your example.

In the line: Element element (Element) node; Dont cast type Element to Node. Please help me. Regrads.