Discussions

Web tier: servlets, JSP, Web frameworks: Generate XML file from database

  1. Generate XML file from database (2 messages)

    I have downloaded the latest version of xerces (xml parser).

    I am trying out an example from Beginning Java Databases book.

    I have set the classpath as c:\Xerces-J-bin.1.4.4\xerces-1_4_4\xerces.jar;

    The file below compiles but I am not able to run it. It says that No Class Defination is found. What am I missing in the classpath?

    import java.sql.*; /* JDBC Classes*/
    import java.io.*; /* Java IO */
    import org.w3c.dom.*; /* W3C Interfaces */
    import org.apache.xerces.dom.*; /* Xerces DOM Classes */
    import org.apache.xml.serialize.*; /* Xerces Serializer */


    public class databasexml {

      public static final String JDBCURL = "oracle.jdbc.driver.OracleDriver";
      public static final String JDBCDRIVER ="jdbc:oracle:thin:@wcsuds1.ctstateu.edu:1521:wprac";
      public static final String SQL = "select customerid, customerfirstname, customerlastname from customers";
      public static String OUTPUTFILE = "c:
    customers.xml";

      public static void main(String[] args) {

        try{

        /** Step 1 : Making a JDBC Connection with database" **/
        Class.forName(JDBCDRIVER).newInstance();
        Connection conn = DriverManager.getConnection(JDBCURL);

        /** Step 2 : Retrieve the customer data from database **/
        Statement statement = conn.createStatement();
        ResultSet customerRS = statement.executeQuery(SQL);

        /** Step 3 : Build customer XML DOM **/
        Document xmlDoc = buildCustomerXML(customerRS);

        /** Step 4 : Write output to a file **/
        File outputFile = new File(OUTPUTFILE);
        printDOM(xmlDoc, outputFile);

        conn.close(); /*Connection close*/
        }
        catch(Exception e)
        {
          System.out.println("Really poor exception handling " +e.toString());
        }
      }//Main

      /*Build XML DOcument from database. The XML object is returned to main method where it is written to flat file.*/
      private static Document buildCustomerXML(ResultSet _customerRS) throws Exception
      {

      Document xmlDoc = new DocumentImpl();

      /* Creating the root element */
      Element rootElement = xmlDoc.createElement("CUSTOMERS");
      xmlDoc.appendChild(rootElement);

      while(_customerRS.next())
       {
        Element customer = xmlDoc.createElement("CUSTOMER");

        /* Build the CustomerId as a Attribute*/
        customer.setAttribute("customerid", _customerRS.getString("customerid"));

        /* Creating elements within customer DOM*/
        Element firstName = xmlDoc.createElement("FIRSTNAME");
        Element lastName = xmlDoc.createElement("LASTNAME");

        /* Populating Customer DOM with Data*/
        firstName.appendChild(xmlDoc.createTextNode(_customerRS.getString("customerfirstname")));
        lastName.appendChild(xmlDoc.createTextNode(_customerRS.getString("customerlastname")));

        /* Adding the firstname and lastname elements to the Customer Element*/
        customer.appendChild(firstName);
        customer.appendChild(lastName);

        /* Appending Customer to the Root Class*/
        rootElement.appendChild(customer);
       }
      return xmlDoc;
      }

      /* printDOM will write the contents of xml document passed onto it out to a file*/
      private static void printDOM(Document _xmlDoc, File _outputFile) throws Exception
      {
        OutputFormat outputFormat = new OutputFormat("XML","UTF-8",true);
        FileWriter fileWriter = new FileWriter(_outputFile);

        XMLSerializer xmlSerializer = new XMLSerializer(fileWriter, outputFormat);

        xmlSerializer.asDOMSerializer();

        xmlSerializer.serialize(_xmlDoc.getDocumentElement());
      }

     }

    Threaded Messages (2)

  2. Generate XML file from database[ Go to top ]

    You will need classes12.jar (If you are using Oracle 8i).
    Hope this helps.
    Sudhakar.

  3. Generate XML file from database[ Go to top ]

    You may have set your classpath like this :

    c:\Xerces-J-bin.1.4.4\xerces-1_4_4\xerces.jar;C:\xxx\classes12.zip;.

    Replace xxx with your Oracle directory and as shown above
    you will need a . in your classpath

    Thanks,
    Sudhakar.