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:-
/*
|
Output of the program:-
Name : Girish Tewari City : Haldwani PhoneNo:1234567890 Name : Mahendra Singh City : Lucknow PhoneNo:123652314 |