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

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"?>
<Company>
  <Employee>
  <name>Girish Tewari
  </name>
  <City>Haldwani</City>>
  <Phoneno>1234567890</Phoneno>
  </Employee>
  <Employee>
  <name>Mahendra Singh
  </name>
  <City>Lucknow</City>
  <Phoneno>123652314</Phoneno>`
  </Employee>
</Company>

GettingTextValues.java:-

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

import java.io.*;
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 = 
  element4.getElementsByTagName(
"Phoneno");
  Element element5 = (Element) nodelist2.item(0);
  NodeList fstNm2 = element5.getChildNodes();
  System.out.println("PhoneNo:" 
  + (fstNm2.item(
0)).getNodeValue());
  }
  }
  }
}
  


Output of the program:-

Name : Girish Tewari
  City : Haldwani
PhoneNo:1234567890
Name : Mahendra Singh
  City : Lucknow
PhoneNo:123652314


Download Source Code