First, JDOM introduction
We know that the DOM is used to represent the official W3C standard of the XML documentation as a platform and language, using the DOM and SAX API to resolve and process XML documents. Here we introduce JDoms based on tree operation, should say that it provides a solution for resolution, creation, processing, and implementing XML. These APIs are more intuitive than the Methods provided by the DOM and SAX API. For programmers with Java experience, JDOM will find that JDOM is very easy to master. JDOM processes XML more than DOMs, and its function is more powerful than using SAX.
The internal logic structure of JDOM is basically the same as DOM, such as document node type, such as Document, Element, Comment, where each JDOM document must have a Document node and the root node of the node tree. This root node can have sub-nodes or leaves nodes such as Comment, Text, etc. Each node type in the JDOM document corresponds to a good element in the XML text in the format. This also provides operational basis for our use of JDOM conversion databases to XML documents.
The advantages of JDOM:
Declaring by JDOM, we will see the advantages of application JDom. JDOM documentation is as follows, "JDOM references 20/80 principles," 10% energy to solve 80% Java / XML problem ".
● JDOM is developed with Java and serves Java, which uses Java code specification and class libraries;
● In numerous programming languages, Java is an excellent platform using XML, and XML is an excellent data representation of Java applications. JDOM API is a pure Java API is easier for Java developers;
● JDOM API is more intuitive than the method provided by the DOM and simplifies interaction with XML. Faster than using DOM.
Org.jdom is a JDM toolkit for Java API operations.
In Org.jdom, Document, Element, Comment, Doctype, Attribute, Text, etc. are provided, which are all necessary to access and operate the JDOM documentation. We can use these classes to create, traverse, and modify the JDOM documentation.
In Org.jdom.Output, domoutputter, XMLOUTPUTTER is provided to handle the DOM tree form of the JDOM tree, and the XML document is output, printing, etc.
Second, environmental configuration
In my Windows2000 system platform, Tomcat4.1.18, JDK1.4.0_02 is used as the development and test platform.
The "Advanced" attribute of "My Computer" is added to: ".; C: /J2SDK1.4.0_02/lib/dt.jar; c: /j2sdk1.4.0_02/lib/tools. jar; ". Path is set to "; C: /J2SDK1.4.0_02/bin;".
SQL Server JDBC driver: MSSQLServer.jar is placed in the Tomcat's lib directory.
Third, get and install JDOM
Since JDO is not included in the JDK in Sun (I want JDom, Jdom I will be part of the Sun JDK), we must manually download and set up a JDOM.
You can download the latest version of JDOM at http://www.jdom.org. Download the jdom beta8 here. Download JDOM-B8.ZIP After completion, JDOM's JAR file is the file jdom.jar in the build directory, copy the above file to the JRE / lib / ext directory in the J2SDK1.4.0_02 directory.
Fourth, using JDOM to implement SQL Server database to XML conversion
1, the creation of the database, table
An example of a conversion customer information is applied here - "Customer Basic Information". We are placed in the Microsoft SQL Server 2000 database and then dynamically generate an XML instance document using JSP and JDOM technology.
We created a data table in the Microsoft SQL Server Database Custom Custom, and its data structure is shown in the following table:
Field name
Comment
Types of
Is it empty?
Name
Name
VARCHAR (12)
NOT NULL
Id
Id
int
NOT NULL
Company
the company
VARCHAR (30)
NOT NULL
Tel
Varchar (15)
NOT NULL
Enter the following records into the database:
Name
Id
Company
Liu's
001
Company 1
Liu@yantai.com
Jiang's
002
Company 2
Jiang@yantai.net
2, data source settings
The data source (ODBC Source) is actually the source of defining the data. The setting method of the data source is: [Start] -> [Settings] -> [Control Panel] -> [Management Tool] -> [Data Source (ODBC)] -> [SYSTEM DSN] -> [Add] -> [ SQL Server], configure server name (Custom), Database Name, Data Source Name (DB_CUSTOM), username (LGZ), user password (empty), data source is configured.
3, code writing
Let's start our JSP code conversion work, the code will generate an XML structure by calling the JDOM, and then access the SQL Server database dynamically populate XML content with the JDBC.
<% @ page contenttype = "text / html; charset = GBK"%>
HEAD>
<% @ page import = "org.jdom. *"%>
<% @ Page Import = "Java. *"%>
<%
Class.Forname ("com.microsoft.jdbc.sqlserver.sqlserverdriver). NewInstance ();
String Url = "JDBC: Microsoft: SQLServer: //10.40.14.54: 1433; DatabaseName = DB_CUSTOM";
/ / Load JDBC ODBC driver
String User = "LGZ";
String password = "lgz";
Connection conn = drivermanager.getConnection (URL, User, Password); / / Connecting Database
Statement Stmt = Conn.createStatement (ResultSet.Type_Scroll_Sensitive, ResultSet.concur_Updata);
// Create a Statement
String SQL = "SELECT * from client"; // Define the SQL statement of the query
ResultSet RS = Stmt.executeQuery (SQL); // Execute Query
Document Document = New Document (New Element); // Create a document
ResultSetMetadata RSMD = rs.getMetadata (); // Get field name int number = rsmd.getColumnCount (); // Get field number
INT i = 0;
While (rs.next ()) {// Remove the query results
Element Element0 = New Element; // Creating an element to generate a JDOM tree
Document.getrootElement (). AddContent (Element0);
For (i = 1; i <= numberofcolumn; i )
{String Date = new string (rs.getstring (i) .GetBytes ("ISO-8859-1"), "GB2312"); // Code Conversion
Element Element = New Element (rsmd.getcolumnname (i)). SetText (Date);
Element0.addContent (Element);
}
}
rs.close (); // Close result set
Stmt.close (); // Close Statement
Conn.close (); // Close connection
XmloutPutter outp = new xmloutputter ();
Outp.output (Document, New FileoutputStream ("D: //Data.xml")); // Output XML document
Out.print ("XML document is generated!");
%>
Click to open the resulting XML document
Body>
Html>
4, the operation of the program
Run the JSP through the 8080 port of the browser, the expected XML file will be generated.
V. Application analysis
XML as a standard for data exchange, it has received more and more extensive applications. This article clarifies the basic method of databases to XML conversion, thereby making data exchange for each heterogeneous platform and various formats. When we can convert the original data into XML formatted form, we can expressed in a rich HTML format. Here is the application of two aspects:
1, XML application server:
The XML application server is actually a web application server that supports XML, which is usually template-driven, and extracts data by embedding using SQL statements in a scrip language and dynamically builds XML documents.
2, XML-based desktop application
We know that the same data can be submitted to end customers in different data, and an XSL file describes the display of data, you can connect many XSL and the same XML document to provide different HTML-based representations, so In fact, we can build XML-based desktop applications
This method mainly has two advantages. First, you can operate data in a platform and language independent way, follow, you don't need to program a different view of the same data.