Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Getting text values from a NodeList

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


JAXB Create XML File And Get Data From XML XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials The Simple API for XML (SAX) APIs Part I XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials XML Tutorial Ignoring Comments While Parsing an XML File Accessing XML file from Java Preventing Expansion of Entity References Getting next Tag in the XML File Replacing a Node with a New One Reading XML Data from a Stream Reading an XML document using JDOM Query XML with an XPath expression Normalize All of the Text in a Document Modifying Text by Replacement Insert a Processing Instruction and a Comment Node Getting text values from a NodeList Finding an ID Value and Print the Element Text Edit Text by Insertion and Replacement Duplicating a Subtree Deleting Two Attributes Delete the First Child of the Root Node Creating XML Tree Creating a DocumentFragment Subtree and Appending it to the Document Creates element node, attribute node, comment node, processing instruction and a CDATA section Creates a New DOM Parse Tree Replacing a Text Node with a New CDATA Section Node Create an Empty DOM Document Adding an Attribute in DOM Document Transforming XML with XSLT Transforming an XML File with XSL Transforming XML with SAXFilters Storing properties in XML file Testing EntityReferences in Xml Splitting One Text Node into Three Reading XML from a File Modifying Text by Cutting and Pasting Locating a Node by Using Siblings Locate a Node and Change Its Content Loading properties from a XML file Listing nodes used in a document Inserting a New Entry in a List Creating an InlineStreamFilter Ignoring Comments While Parsing an XML File Handling Errors While Parsing an XML File Emitting DOCTYPE Declaration while writing XML File Converting CDATA Nodes into Text Nodes Java DOM Tutorial Creating Blank 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.