http://www-1.ibm.com/servers/eserver/iseries/software/websphere/wsappserver/docs/as400v35SimpChineseStd/docs/xmlgen.html看看,很纤细
解决方案 »
- 读源码的伤心人
- jsp
- 我想了解sap pcm,oracle hpcm,sas abm三种产品的优势和劣势。在线等(急)
- 在线求如何改变web页面中下拉式list内容?
- 对J2EE学习的一些疑惑,请高手解答!
- 高深的问题!·动态实例化对象 探讨。。。。。
- 哪里能够下载eclipse 3.0的tomcat插件和struts插件?
- 紧急求助-关于Web-Mail的问题?
- ◆◆哪里有sun petstore的构架介绍的文档或连接? sun 的网站好像没有??
- EJB的问题,希望用EJB做过开发的高手回答。
- struts的例子struts-example为什么不能运行,急,请高手赐教!!!!!!!!!
- 怎样通过DOM机制创建XML文档?
// 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());
}
}