In this section you will study how to generate dynamic XML using JSP.
JSP technology is basically used for building HTML pages with dynamic content but you can use it to generate dynamic content in other formats as well, including XML.
Understand with Example
The Tutorial illustrate an elaborate example on JSP Write XML. To understand the example we make use of java bean that include the string type variables. The java bean can make use of these variables by using setter ( ) and getter ( ) method. The setter ( ) and getter ( ) method are used to define the property of class variable.
set xxx( ) : The set xxx ( ) is used to set the values in variable.
get xxx( ) : The get xxx ( ) is used to access the values from the set xxx ( ) method.
You can see in the given example that we have used xml document in a jsp page and therefore we need to indicate the JSP page compiler by using a JSP page directive tag to set the content type 'text/xml'. The page compiler will use the attributes of the <jsp:useBean> tag to build the Java code to instantiate the bean and populate it with data from the request. The scriplets <% out.print(xml.getId()); %>insert the results into the XML document from the Bean.
Here is the code of GenerateXml.java
Here is the code of xml.jsp
|<?xml version="1.0" encoding="ISO-8859-1"?>
<%@ page contentType="text/xml;charset=ISO-8859-1" %>
<jsp:useBean id="xml" class="form.GenerateXml"/>
<Employee_Id><% out.print(xml.getId()); %></Employee_Id>
<Employee_Name><% out.print(xml.getName()); %></Employee_Name>
<Employee_Address><% out.print(xml.getAddress()); %></Employee_Address>
<ContactNo><% out.print(xml.getContactNo()); %></ContactNo>
Output will be displayed as:
Rs. 20,000 US$ 300
Today: Rs. 10,000 US$150
Course Duration: 30 hrs
Posted on: June 16, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles