• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Weird Question

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do people create xml files in real time environments ?
Say I have a table with data and want to create an xml file .
Do I query the table and write down elemnts in a loop to a file ?
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
That is possible by using jsp..U can pass drivername,database url,username,password and sql as parameters to a bean from the jsp file and get the xml document..
<jsp:useBean id="mybean" scope="page" class="xmlconn"/>
<%!
String driver="your driver";
String url="your url";
String user="username";
String pwd="password";
String sql="your sql query";
ResultSet rs=null;
ResultSetMetaData rsmd=null;
int temp;
%>
<resultset>
<%


rs=mybean.getResult(driver,url,username,pwd,sql);
rsmd=rs.getMetaData();
temp=rsmd.getColumnCount();

while (rs.next())
{

%>
<row>
<%
for(int i=1;i<=temp;i++)
{
String fieldName=rsmd.getColumnName(i);
String fieldValue=rs.getString(i);
%>
<<%=fieldName%>><%=fieldValue%></<%=fieldName%>>
<%
}
%>
</row>
<%
}
%>
</resultset>
In the aboove code, 'xmlconn' is a bean which contains getResult method to process the sql and returns the ResultSet to the jsp.
I've followed this method to create xml documents..
If u find any doubt or difficulty on this pls.let me know
Best Regards,
Paramaguru
 
Lakshmi Anantharaman
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response .
One more question . How do you save such an xml file ? .
Lakshmi

Originally posted by Parama guru:
Hi,
That is possible by using jsp..U can pass drivername,database url,username,password and sql as parameters to a bean from the jsp file and get the xml document..
<jsp:useBean id="mybean" scope="page" class="xmlconn"/>
<%!
String driver="your driver";
String url="your url";
String user="username";
String pwd="password";
String sql="your sql query";
ResultSet rs=null;
ResultSetMetaData rsmd=null;
int temp;
%>
<resultset>
<%


rs=mybean.getResult(driver,url,username,pwd,sql);
rsmd=rs.getMetaData();
temp=rsmd.getColumnCount();

while (rs.next())
{

%>
<row>
<%
for(int i=1;i<=temp;i++)
{
String fieldName=rsmd.getColumnName(i);
String fieldValue=rs.getString(i);
%>
<<%=fieldName%>><%=fieldValue%></<%=fieldName%>>
<%
}
%>
</row>
<%
}
%>
</resultset>
In the aboove code, 'xmlconn' is a bean which contains getResult method to process the sql and returns the ResultSet to the jsp.
I've followed this method to create xml documents..
If u find any doubt or difficulty on this pls.let me know
Best Regards,
Paramaguru


 
Paramagurusamy Balasubramanian
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi lakshmi,
U assume the above file as "xmldb.jsp"..
Now write 1 more jsp called "xmlwrite.jsp",which processes the output from xmldb.jsp through URL object and creates the file..Here is my sample code for xmlwrite.jsp
driver=request.getParameter("driver");
url=request.getParameter("url");
user=request.getParameter("user");
pwd=request.getParameter("pwd");
table=request.getParameter("table");

try
{
sql="select * from "+table;
sql=sql.replace(' ','+');
String fileloc="../webapps/ROOT/xml/"+table+".xml";

FileOutputStream file=new FileOutputStream(fileloc);
String pp="http://localhost:8080/xmlshop/xmldb.jsp?driver="+driver+"&url="+url+"&user="+user+"&pwd="+pwd+"&sql="+sql;

URL urlstr=new URL(pp);

BufferedReader buf=new BufferedReader(new InputStreamReader(urlstr.openStream()));
String in;
while ((in = buf.readLine()) != null)
{
byte[] b=in.getBytes();
file.write(b);
}
out.println("File created Successfully");
buf.close();
file.close();
}
catch(Exception e)
{
out.println("Error :"+ e);
}

%>
I am calling this xmlwrite.jsp from a html file,which gets five inputs from the user namely driver name,database url,user name,password and table name. This xmlwrite.jsp again calls xmldb.jsp via URL and gets back the xml document.After that the xmlwrite.jsp writes the document in a file..
suppose u enter the table name as customer then your file will be stored as ../webapps/ROOT/xml/customer.xml. in the tomcat application server..
Oops!..Confused?..If you go thru both the codes again then u will understand..
Best Regards,
Paramaguru.
 
Hang a left on main. Then read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic