aspose file tools*
The moose likes XML and Related Technologies and the fly likes Weird Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Weird Question " Watch "Weird Question " New topic
Author

Weird Question

Lakshmi Anantharaman
Ranch Hand

Joined: Aug 01, 2001
Posts: 58
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 ?
Paramagurusamy Balasubramanian
Ranch Hand

Joined: Aug 17, 2001
Posts: 55
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

Joined: Aug 01, 2001
Posts: 58
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

Joined: Aug 17, 2001
Posts: 55
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.
 
 
subject: Weird Question