Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Java get Node Value 
 

In this section, you will learn how to obtain the node value. Before any further processing, you need a XML file. For this we have create a employee.xml file.

 

Java get Node Value

                         

In this section, you will learn how to obtain the node value. Before any further processing, you need a XML file. For this we have create a employee.xml file. Now to read the 'employee.xml' file, we have used DocumentBuilderFactory to enable application to obtain a parser  Then the DocumentBuilder  parse and build the XML document. The Document class access the document's data.

builder.parse(file)- This method parse the content of 'employee.xml' file as an XML document an return the DOM object.
getDocumentElement()- This method allows direct access to the child node that is the root element.
getNodeName()- This method returns the node name.
doc.getElementsByTagName("employee")-This method returns the list of nodes of all the Elements with a given tag name.
getChildNodes()-This method provides all the children of this node.
getNodeValue()- This method returns the node value.

Here is the 'employee.xml' file

<?xml version="1.0" encoding="UTF-8"?>
<company>
<employee>
<firstname>Anusmita</firstname>
<lastname>Singh</lastname>
</employee>
</company>

Here is the code of GetNodeValue

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

public class GetNodeValue {
    public static void main(String[] args) throws Exception {
        File file = new File("employee.xml");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(file);
        new GetNodeValue().GettingText(doc);
    }
    public void GettingText(Document doc) {
       Element e = 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("firstname");
                Element element1 = (Element) nodelist.item(0);
                NodeList fstNm = element1.getChildNodes();
                System.out.print("First Name : " + (fstNm.item(0)).getNodeValue());
               }
        }
    }
}     

Output will be displayed as:

Download Source Code

                         

» View all related tutorials
Related Tags: java c error com diff io user sed print output stream display command page exec uri int exe move if

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.