📄 simplexmldata.java
字号:
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 SimpleXMLData {
public static final String JDBCURL =
"jdbc:cloudscape:c:/wrox/database/Wrox4370.db";
public static final String JDBCDRIVER = "COM.cloudscape.core.JDBCDriver";
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);
/**
* ****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());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -