fpoyavo
asked on
Oracle XML
Hi Experts,
I have JSP that retrieves data from table and I would save it as XML file.
Is there any way to do that ?
Thank you.
I have JSP that retrieves data from table and I would save it as XML file.
Is there any way to do that ?
Thank you.
ASKER
Would you do me a favor to make it as JSP. Please. I just would learn and see difference between these two.
<%@ page import="java.sql.* "%>
<%@ page import="javax.xml.* "%>
<%@ page import="org.w3c.dom.* "%>
<%@ page import="de.jeckle.RS2DOM.R S2DOM "%>
<%
//Connect to the Oracle Database, you will have to download the Oracle JDBC driver for this.....
try{
Class.forName("oracle.jdbc .driver.Or acleDriver ");
Connection con = DriverManager.getConnectio n( "jdbc:oracle:thin:@test123 .server.co m:1111:app test", "abc",
"123");
stmt = (Statement) con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM test");
//transfer ResultSet into XML Schema stored as DOM
Document xsd = RS2DOM.ResultSet2XSDDOM(rs );
//transfer ResultSet into DOM
Document d = RS2DOM.ResultSet2DOM(rs);
// You have the XML loaded in Document object d now... you can do whatever you want to do with it...
// here in this example I am writing it to a file on your harddisk..
transformer = tFactory.newTransformer();
transformer.setOutputPrope rty( OutputKeys.INDENT, "yes" );
transformer.setOutputPrope rty("{http://xml.apache.org/xslt}ind ent-amount ", "2" );
File f=new File(c:\\test.xml");
transformer.transform( new StreamSource(new StringReader(d)), new StreamResult(f) );
}
catch(Exception e)
{
e.printStaceTrace();
}
%>
Here you will have to make sure that you have a Oracle JDBC driver on your webserver and you have downloaded the class from the above url that I have posted...
<%@ page import="javax.xml.* "%>
<%@ page import="org.w3c.dom.* "%>
<%@ page import="de.jeckle.RS2DOM.R
<%
//Connect to the Oracle Database, you will have to download the Oracle JDBC driver for this.....
try{
Class.forName("oracle.jdbc
Connection con = DriverManager.getConnectio
"123");
stmt = (Statement) con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM test");
//transfer ResultSet into XML Schema stored as DOM
Document xsd = RS2DOM.ResultSet2XSDDOM(rs
//transfer ResultSet into DOM
Document d = RS2DOM.ResultSet2DOM(rs);
// You have the XML loaded in Document object d now... you can do whatever you want to do with it...
// here in this example I am writing it to a file on your harddisk..
transformer = tFactory.newTransformer();
transformer.setOutputPrope
transformer.setOutputPrope
File f=new File(c:\\test.xml");
transformer.transform( new StreamSource(new StringReader(d)), new StreamResult(f) );
}
catch(Exception e)
{
e.printStaceTrace();
}
%>
Here you will have to make sure that you have a Oracle JDBC driver on your webserver and you have downloaded the class from the above url that I have posted...
ooops
>>> transformer.transform( new StreamSource(new StringReader(d)), new StreamResult(f) );
will be
transformer.transform( new StreamSource(d), new StreamResult(f) );
>>> transformer.transform( new StreamSource(new StringReader(d)), new StreamResult(f) );
will be
transformer.transform( new StreamSource(d), new StreamResult(f) );
ASKER
You are the best man I should say. Let me try that.
ASKER
I have gotten message:
Class de.jeckle.RS2DOM.RS2DOM not found in import.
Where do I get this class ?
Class de.jeckle.RS2DOM.RS2DOM not found in import.
Where do I get this class ?
http://www.jeckle.de/freeStuff/JDBC2XML/
This page have a download there...
download the java file and compile it...
it should be placed in directory structure
web-inf/classes/de/jeckle/ RS2DOM/RS2 DOM
This page have a download there...
download the java file and compile it...
it should be placed in directory structure
web-inf/classes/de/jeckle/
ASKER
Another problem:
Class org.apache.jsp.Transformer not found.
Undefined variable or class name: tFactory
tFactory.newTransformer();
Class org.apache.jsp.File not found.
File f=new File("c:\\test.xml");
Method printStaceTrace() not found in class java.lang.Exception.
e.printStaceTrace();
Man please help.
Class org.apache.jsp.Transformer
Undefined variable or class name: tFactory
tFactory.newTransformer();
Class org.apache.jsp.File not found.
File f=new File("c:\\test.xml");
Method printStaceTrace() not found in class java.lang.Exception.
e.printStaceTrace();
Man please help.
I have modifed the jsp for you.... all were my tayping mistakes...:-)
If you still get the error>>Class org.apache.jsp.Transformer not found
then you do not have XML parser in your system ( though its quite unlikely)..
so give it a shot and let me know...
<%@ page import="java.sql.* "%>
<%@ page import="javax.xml.* "%>
<%@ page import="org.w3c.dom.* "%>
<%@ page import="java.io.* "%>
<%@ page import="de.jeckle.RS2DOM.R S2DOM "%>
<%
//Connect to the Oracle Database, you will have to download the Oracle JDBC driver for this.....
try{
Class.forName("oracle.jdbc .driver.Or acleDriver ");
Connection con = DriverManager.getConnectio n( "jdbc:oracle:thin:@test123 .server.co m:1111:app test", "abc",
"123");
stmt = (Statement) con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM test");
//transfer ResultSet into XML Schema stored as DOM
Document xsd = RS2DOM.ResultSet2XSDDOM(rs );
//transfer ResultSet into DOM
Document d = RS2DOM.ResultSet2DOM(rs);
// You have the XML loaded in Document object d now... you can do whatever you want to do with it...
// here in this example I am writing it to a file on your harddisk..
TransformerFactory tFactory = TransformerFactory.newInst ance();
Transformer transformer;
transformer = tFactory.newTransformer();
transformer.setOutputPrope rty( OutputKeys.INDENT, "yes" );
transformer.setOutputPrope rty("{http://xml.apache.org/xslt}ind ent-amount ", "2" );
File f=new File(c:\\test.xml");
transformer.transform( new StreamSource(new StringReader(d)), new StreamResult(f) );
}
catch(Exception e)
{
e.printStackTrace();
}
%>
If you still get the error>>Class org.apache.jsp.Transformer
then you do not have XML parser in your system ( though its quite unlikely)..
so give it a shot and let me know...
<%@ page import="java.sql.* "%>
<%@ page import="javax.xml.* "%>
<%@ page import="org.w3c.dom.* "%>
<%@ page import="java.io.* "%>
<%@ page import="de.jeckle.RS2DOM.R
<%
//Connect to the Oracle Database, you will have to download the Oracle JDBC driver for this.....
try{
Class.forName("oracle.jdbc
Connection con = DriverManager.getConnectio
"123");
stmt = (Statement) con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM test");
//transfer ResultSet into XML Schema stored as DOM
Document xsd = RS2DOM.ResultSet2XSDDOM(rs
//transfer ResultSet into DOM
Document d = RS2DOM.ResultSet2DOM(rs);
// You have the XML loaded in Document object d now... you can do whatever you want to do with it...
// here in this example I am writing it to a file on your harddisk..
TransformerFactory tFactory = TransformerFactory.newInst
Transformer transformer;
transformer = tFactory.newTransformer();
transformer.setOutputPrope
transformer.setOutputPrope
File f=new File(c:\\test.xml");
transformer.transform( new StreamSource(new StringReader(d)), new StreamResult(f) );
}
catch(Exception e)
{
e.printStackTrace();
}
%>
ASKER
I get this now :
Class org.apache.jsp.Transformer Factory not found.
TransformerFactory tFactory = TransformerFactory.newInst ance();
We have XML installed on this server. I ran another JSP having XML processing and it went fine.
Thank you.
Class org.apache.jsp.Transformer
TransformerFactory tFactory = TransformerFactory.newInst
We have XML installed on this server. I ran another JSP having XML processing and it went fine.
Thank you.
Add this import to the jsp
<%@ page import="javax.xml.transfor m.* "%>
and then test it out
<%@ page import="javax.xml.transfor
and then test it out
ASKER
Yep right there :)
The only one left is :
Method printStaceTrace() not found in class java.lang.Exception.
e.printStaceTrace();
Thank you.
The only one left is :
Method printStaceTrace() not found in class java.lang.Exception.
e.printStaceTrace();
Thank you.
I think I changed it to....
catch(Exception e)
{
e.printStackTrace();
}
%>
catch(Exception e)
{
e.printStackTrace();
}
%>
ASKER
Man we are close :)
Can't convert org.w3c.dom.Document to java.lang.String.
transformer.transform( new StreamSource(new StringReader(d)), new StreamResult(f) );
pointing to (d)
Can't convert org.w3c.dom.Document to java.lang.String.
transformer.transform( new StreamSource(new StringReader(d)), new StreamResult(f) );
pointing to (d)
ASKER
More details : Incompatible type for constructor.
try changing this line to
transformer.transform( new StreamSource(d), new StreamResult(f) );
if this does not work try
transformer.transform( new DOMSource(d), new StreamResult(f) );
transformer.transform( new StreamSource(d), new StreamResult(f) );
if this does not work try
transformer.transform( new DOMSource(d), new StreamResult(f) );
if both does not work then try
Can't convert org.w3c.dom.Document to java.lang.String.
transformer.transform( new StreamSource(new StringReader(org.apache.so ap.util.xm l.DOMWrite r.nodeToSt ring(d))), new StreamResult(f) );
This one is bound to work
Can't convert org.w3c.dom.Document to java.lang.String.
transformer.transform( new StreamSource(new StringReader(org.apache.so
This one is bound to work
ASKER
oK.....iT seems doing something but there is no error meesages nor test.xml on my c: drive
do
System.out.println(org.apa
just before the transform and see if it prints anything on the tomcat console...
ASKER
Nothing at all ....just blank jsp after processing no error, no file ??? :) really getting strange
Post your jsp here, also what web server you are using???
ASKER
It is RED HAT 8, TOMCAT
<%@ page import="java.sql.* "%>
<%@ page import="java.io.* "%>
<%@ page import="javax.xml.transfor m.*"%>
<%@ page import="javax.xml.transfor m.stream.* "%>
<%@ page import="org.w3c.dom.* "%>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="de.jeckle.RS2DOM.R S2DOM"%>
<%
Connection con = null;
try
{
Class.forName("oracle.jdbc .driver.Or acleDriver ");
con = DriverManager.getConnectio n(
"jdbc:oracle:thin:@192.101 .201.20:15 21:ORATEST ",
"TEST",
"TEST");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from test;");
Document xsd = RS2DOM.ResultSet2XSDDOM(rs );
Document docout = RS2DOM.ResultSet2DOM(rs);
TransformerFactory tfactory = TransformerFactory.newInst ance();
Transformer transformer = null;
transformer = tfactory.newTransformer();
transformer.setOutputPrope rty( OutputKeys.INDENT, "yes" );
transformer.setOutputPrope rty("{http://xml.apache.org/xslt}ind ent-amount ", "2" );
File myfile = new File("c:\\temp\\test.xml") ;
System.out.println(org.apa che.soap.u til.xml.DO MWriter.no deToString (docout));
transformer.transform( new StreamSource(new StringReader(org.apache.so ap.util.xm l.DOMWrite r.nodeToSt ring(docou t))), new StreamResult(myfile) );
}
catch(Exception e)
{
e.printStackTrace();
}
%>
<%@ page import="java.sql.* "%>
<%@ page import="java.io.* "%>
<%@ page import="javax.xml.transfor
<%@ page import="javax.xml.transfor
<%@ page import="org.w3c.dom.* "%>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="de.jeckle.RS2DOM.R
<%
Connection con = null;
try
{
Class.forName("oracle.jdbc
con = DriverManager.getConnectio
"jdbc:oracle:thin:@192.101
"TEST",
"TEST");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from test;");
Document xsd = RS2DOM.ResultSet2XSDDOM(rs
Document docout = RS2DOM.ResultSet2DOM(rs);
TransformerFactory tfactory = TransformerFactory.newInst
Transformer transformer = null;
transformer = tfactory.newTransformer();
transformer.setOutputPrope
transformer.setOutputPrope
File myfile = new File("c:\\temp\\test.xml")
System.out.println(org.apa
transformer.transform( new StreamSource(new StringReader(org.apache.so
}
catch(Exception e)
{
e.printStackTrace();
}
%>
1. Do you have a table with Name "test" in your database?
2. does it have data in it?
3. On red HAT C:\\temp\\Test.xml not gonna work.... we will have to change it /root/Test.xml
4. try commening the Document xsd = RS2DOM.ResultSet2XSDDOM(rs ); line.. we are not doing anything with it anyway...
5. If it still runs silently and does not report any errors then I will be dammned..:-)
2. does it have data in it?
3. On red HAT C:\\temp\\Test.xml not gonna work.... we will have to change it /root/Test.xml
4. try commening the Document xsd = RS2DOM.ResultSet2XSDDOM(rs
5. If it still runs silently and does not report any errors then I will be dammned..:-)
ASKER
Hi, :)
1 yes I use it for test
2 yes over 10000 rows but I was trying to get more specific data by using where as well and it works fine in another jsp without xml ofcourse
3 I did that too
4 commented out
5 believe it or not .... nothing again.
May be it has to do with
transformer.setOutputPrope rty( OutputKeys.INDENT, "yes" );
transformer.setOutputPrope rty("{http://xml.apache.org/xslt}ind ent-amount ", "2" );
What these parameters do ?
1 yes I use it for test
2 yes over 10000 rows but I was trying to get more specific data by using where as well and it works fine in another jsp without xml ofcourse
3 I did that too
4 commented out
5 believe it or not .... nothing again.
May be it has to do with
transformer.setOutputPrope
transformer.setOutputPrope
What these parameters do ?
those will come in to picture only after it executes the transformer.transform() method..
and I am doing a System.out.println() just before that...
it is bound to put a log or a console msg on the tomcat......
Are you sure your tomcat is showing nothing on the console????????
Or may be putting it in a log file some where????
I am done for the day so try looking for the server logs or server out put of your tomcat... we shd have some thing....
and we will take it from there tomorrow...
and I am doing a System.out.println() just before that...
it is bound to put a log or a console msg on the tomcat......
Are you sure your tomcat is showing nothing on the console????????
Or may be putting it in a log file some where????
I am done for the day so try looking for the server logs or server out put of your tomcat... we shd have some thing....
and we will take it from there tomorrow...
ASKER
In Tomcat logs I have found
004-04-09 01:13:24 jsp: init
2004-04-09 01:15:05 jsp: init
2004-04-09 01:16:11 jsp: init
2004-04-09 01:18:01 jsp: init
2004-04-09 01:21:46 jsp: init
Nothing else.
004-04-09 01:13:24 jsp: init
2004-04-09 01:15:05 jsp: init
2004-04-09 01:16:11 jsp: init
2004-04-09 01:18:01 jsp: init
2004-04-09 01:21:46 jsp: init
Nothing else.
<%@ page import="java.sql.* "%>
<%@ page import="java.io.* "%>
<%@ page import="javax.xml.transfor m.*"%>
<%@ page import="javax.xml.transfor m.stream.* "%>
<%@ page import="org.w3c.dom.* "%>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="de.jeckle.RS2DOM.R S2DOM"%>
<%
Connection con = null;
try
{
Class.forName("oracle.jdbc .driver.Or acleDriver ");
con = DriverManager.getConnectio n(
"jdbc:oracle:thin:@192.101 .201.20:15 21:ORATEST ",
"TEST",
"TEST");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from test;");
Document xsd = RS2DOM.ResultSet2XSDDOM(rs );
Document docout = RS2DOM.ResultSet2DOM(rs);
TransformerFactory tfactory = TransformerFactory.newInst ance();
Transformer transformer = null;
transformer = tfactory.newTransformer();
transformer.setOutputPrope rty( OutputKeys.INDENT, "yes" );
transformer.setOutputPrope rty("{http://xml.apache.org/xslt}ind ent-amount ", "2" );
File myfile = new File("c:\\temp\\test.xml") ;
myfile.close();
out.write(org.apache.soap. util.xml.D OMWriter.n odeToStrin g(docout)) ;
transformer.transform( new StreamSource(new StringReader(org.apache.so ap.util.xm l.DOMWrite r.nodeToSt ring(docou t))), new StreamResult(myfile) );
}
catch(Exception e)
{
e.printStackTrace();
out.write(e);
}
%>
try this one..
<%@ page import="java.io.* "%>
<%@ page import="javax.xml.transfor
<%@ page import="javax.xml.transfor
<%@ page import="org.w3c.dom.* "%>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="de.jeckle.RS2DOM.R
<%
Connection con = null;
try
{
Class.forName("oracle.jdbc
con = DriverManager.getConnectio
"jdbc:oracle:thin:@192.101
"TEST",
"TEST");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from test;");
Document xsd = RS2DOM.ResultSet2XSDDOM(rs
Document docout = RS2DOM.ResultSet2DOM(rs);
TransformerFactory tfactory = TransformerFactory.newInst
Transformer transformer = null;
transformer = tfactory.newTransformer();
transformer.setOutputPrope
transformer.setOutputPrope
File myfile = new File("c:\\temp\\test.xml")
myfile.close();
out.write(org.apache.soap.
transformer.transform( new StreamSource(new StringReader(org.apache.so
}
catch(Exception e)
{
e.printStackTrace();
out.write(e);
}
%>
try this one..
ooopsss..
remove that myfile.close(); its incorrect..
rest of it is good...
remove that myfile.close(); its incorrect..
rest of it is good...
ASKER
I did some of yours and my modification and here is the error message :
errorjava.sql.SQLException : Invalid operation for forward only resultset : isLast
:) but we are close
errorjava.sql.SQLException
:) but we are close
ASKER
I have found it on Net ...may be this is it
Statement stmt = conn.createStatement(Resul tSet.TYPE_ SCROLL_INS ENSITIVE, ResultSet.CONCUR_UPDATABLE );java.sql .ResultSet rs = stmt.executeQuery("SELECT TIME,TT FROM TEST");// it will be scrollable, will not show changes made by others,// and will be updatableint count = 0;while (rs.next()){++count;System .out.print ln ("TestJavaResultSet: Record #"+ count +" " + rs.getString("TIME")); }count = 0;rs.beforeFirst();while (rs.next()){++count;System .out.print ln ("TestJavaResultSet: Record #" + count + " " + rs.getString("TT"));}
Statement stmt = conn.createStatement(Resul
ASKER
And this :
stmt = con.createStatement(Result Set.TYPE_S CROLL_INSE NSITIVE, ResultSet.CONCUR_READ_ONLY );
Is it usefull ?
stmt = con.createStatement(Result
Is it usefull ?
ASKER
Now I am getting : errorjava.sql.SQLException : Exhausted Resultset
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Don't forget to restart the tomcat server...
ASKER
:) I have good news ..it started writing file but inside not that good.
This is what I have got inside ..just a couple of lines and no data
<?xml version="1.0" ?>
<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
That's it.
This is what I have got inside ..just a couple of lines and no data
<?xml version="1.0" ?>
<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
That's it.
ASKER
Man It is ..........WWWWWOOOOOORKIII IING THANKS A LOT.
Glad to hear that.....:-) and thanks for the "A"
Have a look at
http://www.jeckle.de/freeStuff/JDBC2XML/
They have got the code there too...
That code is written in Servlet style but its should not be very hard have it in jsp..