Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
JSP
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Example of printing Text message passed from XML to JSP

                         

In this tutorial we are going to know how we can pass a text message from XML to JSP.  

This example will examine how to parse and expose XML information using the JAXP with a JSP page. This tutorial is only geared towards showing how to construct a Java object from an XML document. For this what we need is a XML file in which we have some text data which needs to be retrieved and inserted into the jsp page. What we need to learn is to retrieve the data from the XML file and insert it into the jsp page.

To make a program over it we have used the following page directive attributes, the first attribute we have used is contentType and the second attribute is import. The content type tells about the MIME type and character encoding the JSP file uses for the response it sends to the client. We need to import some classes and packages which we have described in the tutorial. 

The logic of the program is written inside the scriptlet. Now inside the scriptlet firstly we will define a factory API which allows our application to obtain a Java XML  parser. DocumentBuilderFactory defines a factory API. Now create a DocumentBuilder object to parse an org.w3c.dom.Document from XML. Save the xml file at bin file of  C:\apache-tomat-5.5.23\bin\. For that we need to parse method to actually parse the XML file to create our own Document object. Document object will be created by parse method of the DocumentBuilder

From the Document object we will get a NodeList object represents all of the elements in our XML document name message. NodeList object will be obtained by the getElementByTagName(). 

A NodeList is an array starting from 0 and going up to the length of the array by calling getFirstChild() method. By using this method the desired text node is returned and we can use getNodeValue() for our message. 

Now you can better understand the use of xml. For this program to run we need to import one jar file i.e itext.

Code of the message.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<messages>
<message>Hello Rose India Pvt Ltd</message>
</messages>

Here is the code of the do_message.jsp file:

<%@ page contentType="text/html"%>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory,
javax.xml.parsers.DocumentBuilder,org.w3c.dom.*"
%>
<%
DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("\\message.xml");
NodeList nl = doc.getElementsByTagName("message");
%>
<html>
<body>
<%= nl.item(0).getFirstChild().getNodeValue() %>
</body>
</html>

The output of the program is given below:

Download do_message.jsp file.

Download message.xml file.

 

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

hi,
from where we can get these files for XML parser

javax.xml.parsers.DocumentBuilderFactory,
javax.xml.parsers.DocumentBuilder,org.w3c.dom.*

regards
suwarna

Posted by suwarna on Monday, 12.17.07 @ 15:00pm | #42706

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.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2008. All rights reserved.