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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java get Node Text 
 

In this section, you will learn how to obtain the text of the node in the specified XML file. For this we have create a 'employee.xml' file.

 

Java get Node Text

                         

In this section, you will learn how to obtain the text of the node in the specified XML file. For this we have create a 'employee.xml' file. In the given example, we have used XMLInputFactory class in order to get the streams. The class XMLStreamReader reads the entire XML data in the most efficient way.

streamReader.nextTag()- This method of XMLStreamReader class skips any white space, comment, or processing element, until a start element or end element is reached.

streamReader.next()- This method returns the next parsing event.

streamReader.getText()- This method returns the current value of the event.

Understand with Example

In the given below program code, we want to describe you how to obtain the text of the node in the java application from specified XML File. For this we create a xml file name "employee.xml".In order to get the text of the node specified in a XML file, we have a class name GetNodeText,Inside this class we have a main method in which we assign a memory to the object of file class that accept xml file as argument. The XmlInputFactory  call a new instance method to obtain the streams from a xml file. The next Tag( ) method of Stream Reader class skips white space comments or elements in processing,untill a start element is reached. The next( ) method of Stream Reader class returns you the next parsing events. The get Text( ) returns you the current value of the event .On the Execution of GetNodeText.java you get the text of the node specified in "employee.xml" xml file.

Here is the 'employee.xml'

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

Here is the code of GetNodeText.java

import java.io.File;
import javax.xml.stream.*;
import java.io.FileInputStream;

public class GetNodeText {
    public static void main(String[] args) throws Exception {
        File file = new File("employee.xml");
        XMLInputFactory Factory = XMLInputFactory.newInstance();
        XMLStreamReader streamReader = Factory.createXMLStreamReader
                                               (new FileInputStream(file));
        int nodeId = streamReader.nextTag();
        nodeId = streamReader.nextTag();
        nodeId = streamReader.next();
        nodeId = streamReader.next();
        nodeId = streamReader.next();
        System.out.println("Text of node firstname is: " + streamReader.getText());
        nodeId = streamReader.nextTag();
        nodeId = streamReader.next();
        nodeId = streamReader.next();
        nodeId = streamReader.next();
        System.out.println("Text of node lastname is:  " + streamReader.getText());
    }
}   

Output will be displayed as:

Download Source Code

                         

» View all related tutorials
Related Tags: java c memory time application object io runtime free method get order return this sso ai app with to run

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
Software Solutions
Least Viewed
Most Rated
Recently Viewed
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.