ibm有篇文章,讲的很好,自己去看看把我一千做的时候,也遇到果讲的很详细
解决方案 »
- org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException求助
- 求高手解答。谢谢!
- servlet运行问题!!!
- 如果存储过程返回多记录集?怎么得到某一个
- <jsp:forward page="portal" />这里的portal对应哪里?
- hibernate 分页出错。。。
- value object--javabean中的问题,急需解决!!
- 请各位大虾帮我看一下,关于 全文搜索引擎 的问题(高分相求)。
- birt报表中,以rptconfig结尾的是什么文件?
- 关于javaweb获取客户端ip地址的问题。
- 怎样通过DOM机制创建XML文档?
- EJB部署简单问题--高分
[email protected]
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());
}
}