Breaking News

XML and Server

Generating XML with JSP
 
XML can be generated on a server without any installed XML software.
 
To generate an XML response from the server – simply write the following code and save it as an JSP file on the Tomcat web server and create a System DSN named “simple” that will be used to connect the JSPto the database.
 
The Associate table contains the following fields:
 
Field name Data type
Name Varchar(30)
Age Integer/int
Address Varchar(30)
City varchar(30)
State Varchar(2)
 
<%
java.sql.ResultSet rs;
java.sql.PreparedStatement pst;
java.sql.Connection con;
response.setContentType(“text/xml”);
out.println(“<?xml version=\”1.0\” encoding=\”ISO-8859-1\”?>”);
out.println(“<ebiz>”);
out.println(“<details>”);
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
//Accessing jdbc driver in JSP con=java.sql.DriverManager.getConnection(“jdbc:odbc:simple”);
//here simple is the name of DSN that is used to connect to desired database
pst=con.preapareStatement(“select * from associate”);
//here associate is the name of database table
rs=pst.executeQuery();
if(rs.next())
{
out.println(“<name>”+rs.getString(“name”)+”</name>”);
out.println(“<age>”+rs.getInt(“age”)+”</age>”);
out.println(“<address_str>”+rs.getString(“address”)+”</address_str>”);
out.println(“<city>”+rs.getString(“city”)+”</city>”);
out.println(“<state>”+rs.getString(“state”)+”</state>”);
}
}
catch(Exception e)
{
out.println(“<name>Unable to retrieve details from server </name>”);
}
finally
{
out.println(“</details></ebiz>”);
con.close();
}
%>

Check Also

PCDATA In XML

PCDATA acronyms for Parsed Character Data. Data of this section is parsed by XML Parser …