Friday, August 12, 2011

Converting Jsp/Html Form Values To XML

Requirements:

1. For this i used Castor Framework, it makes my work simple.
   http://www.castor.org/download.html

Go for the  " Full documentation including all JavaDocs and specs (big) "
b'cos it includes all the dependency jar files.

2. Include those Jars project.

Procedure:

Write a Jsp with minimal form elements and calling to servlet or action.
i called action in this example.




<form name="sample" action="AccessDB" method="get">

<table>

<tbody><tr>

<td>CUSTOMER Id</td>

<td> <input type="text" name="custid" value="" size="15" /></td>

</tr>

<tr>

<td>CUSTOMER NAME</td>

<td><input type="text" name="name" value="" size="25" /></td>

</tr>

<tr>

<td>DISCOUNT ID</td>

<td><select name="discountid">

<option>H</option>

<option>M</option>

<option>L</option>

<option>N</option>

</select></td>

</tr>

<tr><td> ZIP</td>

<td><input type="text" name="zip" value="" size="10" /></td>

</tr>

<tr>

<td><input type="submit" value="Submit"/></td>

</tr>

</tbody>

</form>


Here AccessDb is servlet.
below is the servlet code:



CustomerBean cb=new CustomerBean();

cb.setCustid(request.getParameter("custid").toString());

cb.setName(request.getParameter("name").toString());

cb.setDiscountid(request.getParameter("discountid").toString());

cb.setZip(request.getParameter("zip"));




FileWriter writer = new FileWriter("/home/miracle/Desktop/cbs.xml");

Marshaller.marshal(cb, writer);



CustomerBean  is a normal pojo class with normal setters and getters. 

It will create a XMl file on specified path.

<?xml version="1.0" encoding="UTF-8"?>
<customer-bean><discountid>H</discountid><name>Venkat</name><custid>1003</custid><zip>53662</zip></customer-bean>

No comments:

Post a Comment