10 Replies Latest reply on Dec 11, 2003 9:36 AM by 843841

    Reading an XML file

    843841
      I am creating an xml file for mapping appropriate beans. Depending on the request URL i want to map the appropriate beans/jsp pages. I don't know how to read data from an xml file.

      Can anyone tell me how can i do this. what java objects should be used to read elements from xml file?

      Thankyou
      Ram
        • 1. Re: Reading an XML file
          796254
          Look in the javax.xml and org.w3c.dom packages in your Javadocs.

          You'll need an XML parser to take in the data and parse it for you. There are two kinds of parsers: SAX (Simple API for XML) that read the XML, parse the data, and fire off methods for each event (e.g., "start tag", "attribute", "end tag", etc.) You have to override these methods to do what you want.

          The other kind is a DOM parser (Document Object Model). It's really a SAX parser with a handler written for you that creates and populates an in-memory tree for your XML stream. The tree uses classes from the org.w3c.dom package to give you an API for navigating through the tree and manipulating it.

          There's another package called JDOM, written by Jason Hunter and Brett McLaughlin, that some people say is an easier way to manage XML:

          http://www.jdom.org

          I use JDOM and like it very much. - MOD
          • 2. Re: Reading an XML file
            843841
            MOd, Thankyou for your advice........I am trying in the following way........could you spare few seconds to have a look at this........


            I am trying to read data from an XML file. For that i am using oracle.xml.sql.query.OracleXMLQuery class. When i using this ...........i am getting the follwing servlet execution error.

            java.lang.NoClassDefFoundError: oracle/sql/Datum
            at oracle.xml.sql.query.OracleXMLQuery.(OracleXMLQuery.java:155)
            at ReadXML.service(ReadXML.java:23)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

            .....etc

            I have set the classpath to xmlparserv2.jar, oraclexsql.jar, xsu12.jar files.

            Can anyone help me in this regard.........!!!!?????
            Thanks
            Ram
            • 3. Re: Reading an XML file
              843841
              You need to add those JAR files in WEB-INF/lib directory of your Web application..

              Ajay
              • 4. Re: Reading an XML file
                843841
                If u don't know how to mauplate xml ....
                and if it is possible for u to use jdk1.4 higher version then u can very will use JAXB
                which nearly encapsulate all the complexities of handling xml file (Reading/Writing)
                and provides u the Java Objects to work with .. just check it could save ur time !
                • 5. Re: Reading an XML file
                  843841
                  Hi Ayay,

                  i did that too.....but it did not work............i am getting the same servlet exception that

                  exception:

                  javax.servlet.ServletException: Servlet execution threw an exception
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)


                  and

                  root cause

                  java.lang.NoClassDefFoundError: oracle/sql/Datum
                       at oracle.xml.sql.query.OracleXMLQuery.(OracleXMLQuery.java:155)
                       at ReadXML.service(ReadXML.java:23)
                       at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)

                  ........................................................................................................................................................................................

                  I am pasting my servlet program here under for more clarity.

                  import javax.servlet.*;
                  import javax.servlet.http.*;
                  import java.io.*;
                  import java.sql.*;
                  import oracle.xml.sql.query.*;

                  public class ReadXML extends HttpServlet
                  {
                       //Statement stmt;
                       //ResultSet rs;

                       public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
                       {
                            resp.setContentType("text/xml");
                            PrintWriter out = resp.getWriter();
                            String XMLString = "";
                            Connection con;
                            try
                            {
                                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                 con=DriverManager.getConnection("xxxxxxxx","xxxxxxx","xxxxxx");
                                 String dbQuery = "select * from ramtest";
                                 OracleXMLQuery query = new OracleXMLQuery( con, dbQuery );
                                 XMLString = query.getXMLString();               
                                 out.println(XMLString);
                                 con.close();
                                 out.close();
                            }
                            catch(Exception e)
                            {
                                 e.printStackTrace();
                            }
                       }
                  }
                       
                  Hope someone will resolve my problem..............
                  Thanks
                  Ram          
                            
                            
                  • 6. Re: Reading an XML file
                    843841
                    Use jar -tvf <jar-file-name> Command and see if the file exists in the Jar file.

                    Maybe the jar file got damaged during file transfer...

                    • 7. Re: Reading an XML file
                      794117
                      You seem to be using the JDBC-ODBC bridge as your database connection.

                      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                      Is there any reason you aren't using an oracle driver?
                      If oracle provided the package oracle.xml.sql.query.*, I presume that they intend it for use with their driver.
                      You would need to include the Oracle driver in your classpath/lib path.
                      • 8. Re: Reading an XML file
                        796254
                        "...I have set the classpath to xmlparserv2.jar, oraclexsql.jar, xsu12.jar files..."

                        How do you set CLASSPATH?

                        If you're using a system CLASSPATH, be sure that Tomcat is ignoring it completely. Here's the CLASSPATH that Tomcat uses:

                        (1) All the JARs in TOMCAT_HOME/common/lib, which are available to all applications,
                        (2) All the JARs in TOMCAT_HOME/server/lib, which are available only to the server,
                        (3) All the JARs in your application's WEB-INF/lib,
                        (4) All the .class files in your application's WEB-INF/classes.

                        That's it.

                        As a previous poster said, you've got to put those JARs in your WEB-INF/lib directory for Tomcat to find them.

                        You should download all Oracle drivers from this link:

                        http://otn.oracle.com/software/tech/java/sqlj_jdbc/index.html

                        MOD


                        • 9. Re: Reading an XML file
                          843841
                          Mr. MOD,

                          Apart from classpath i have also put the jar files in common/lib and server/lib and also in web-inf/lib folders. Still that was the problem. Might be it is becuase of drivers you said. Only Type-I driver is working with my Tomcat.

                          I have downloaded all the drivers as you said. but i don't know how to install them or where to place them.

                          Could you please advice me in this regard. Thankyou very much for your all adivces given above.

                          Thankyou Very much
                          Ram
                          • 10. Re: Reading an XML file
                            843841
                            Hi Everybody,

                            Thankyou very much for all your cooperation. I am using dom for reading XML file.
                            JSP professional book (wrox publications release2) solved my problem, the chapter 12 on JSP and xml. They illustrated with very simple example very clearly. I found it very useful. Thanks to internet and the users.

                            Users may use this link for reference. In fact, it used DOM concepts.....i was trying in some other way......anyhow it solved my problem.

                            http://java.sun.com/developer/Books/javaserverpages/ProJSP/Chap12.pdf

                            Thanks to everyone who responded to this post. Particularly, Mr. MOD

                            Ram