This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Weird Question  RSS feed

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!