Share on Google+Share on Google+

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.

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"?>
<message>Hello Rose India Pvt Ltd</message>

Here is the code of the do_message.jsp file:

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

The output of the program is given below:

Download do_message.jsp file.

Download message.xml file.




Posted on: March 13, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Example of printing Text message passed from XML to JSP   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image