ibm有篇文章,讲的很好,自己去看看把我一千做的时候,也遇到果讲的很详细

解决方案 »

  1.   

    能不能把这篇文章发到我信箱里,谢谢!急急急...
    [email protected]
      

  2.   

    给你一个例子:
    import java.sql.*;                   /* JDBC Classes */
    // import COM.cloudscape.core.*;        /* Cloudscape JDBC classes */
    import org.w3c.dom.*;                /* W3C Interfaces */
    import org.apache.xerces.dom.*;      /* Xerces DOM Classes */
    import org.apache.xml.serialize.*;   /* Xerces serializer */
    import java.io.*;                    /* Java io classes for file access */public class SimpleXMLDataOracle {  public static final String JDBCURL = 
        "jdbc:oracle:thin:@dbserver:1521:database";
      public static final String JDBCDRIVER = "oracle.jdbc.driver.OracleDriver";
      public static final String SQL = "SELECT customerid, customerfirstname, " +
                                       " customerlastname " +
                                       "FROM customers";
      public static final String OUTPUTFILE = "c:\\customer.xml";
      /**
       * **************************************************************************
       * 
       * This is a simple example of how to pull data out of the database and
       * convert the output to an XML DOM. This class has purposely been written
       * as a set of static methods because at this point I want to focus on the
       * XML code and not necessarily the OO features of Java.
       * ***************************************************************************
       */  public static void main(String args[]) {    try {      /**
           * ****Step 1 - Making my JDBC Connection to Cloudscape****
           */
          Class.forName(JDBCDRIVER).newInstance();
          Connection conn = DriverManager.getConnection(JDBCURL, "beg", "java");      /**
           * ****Step 2 - Retrieve my customer data from the database****
           */
          Statement statement = conn.createStatement();
          ResultSet customerRS = statement.executeQuery(SQL);      /**
           * ****Step 3 - Build customer XML DOM.****
           */
          Document xmlDoc = buildCustomerXML(customerRS);      /**
           * ****Step 4 - Writing the output to a file ****
           */
          File outputFile = new File(OUTPUTFILE);
          printDOM(xmlDoc, outputFile);      conn.close();   /* Closing my connection */
        } catch (Exception e) {
          System.out.println("Really poor exception handling: " + e.toString());
        } 
      }   /**
       * ********************************************************************
       * 
       * The buildCustomerXML will build a simple xml document based on the
       * records retrieved from SQL statement defined in the SQL constant.
       * 
       * The xml document object will then be passed back to the main
       * method where it will be written to a 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");      /* Building the id attribute for the DOM */
          customer.setAttribute("customerid", _customerRS.getString("customerid"));      /* Creating the elements within my customer DOM */
          Element firstName = xmlDoc.createElement("FIRSTNAME");
          Element lastName = xmlDoc.createElement("LASTNAME");      /* Populating my customer DOM with data */
          firstName.appendChild(xmlDoc.createTextNode(
                                _customerRS.getString("customerfirstname")));      lastName.appendChild(xmlDoc.createTextNode(
                               _customerRS.getString("customerlastname")));      /*
           * Appending the customer elements to the customer element declared at
           * the beginning of the while loop.
           */
          customer.appendChild(firstName);
          customer.appendChild(lastName);      /* Appending the customer to the root class */
          rootElement.appendChild(customer);
        }     return xmlDoc;
      }   /**
       * ****************************************************************************
       * 
       * The printDOM method below will write the contents of the XML document
       * passed into it out to the a file. The method will that the XML
       * document will be written out is defined by the File object passed into
       * it.
       * ***************************************************************************
       */
      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());
      } 
    }
      

  3.   

    http://www-1.ibm.com/servers/eserver/iseries/software/websphere/wsappserver/docs/as400v35SimpChineseStd/docs/xmlgen.html去看看就明白了!祝你好运!