First, Jdom Introduction
We know that the DOM is the official consortium of standards for representing XML documents in a platform-and language-independent way, using the DOM and Sax APIs to parse and process XML documents. The jdom here is a pure Java API based on tree operations, and it provides a set of solutions for parsing, creating, processing, and implementing XML. These APIs are more intuitive than the methods provided by the DOM and Sax APIs, and programmers with Java experience will find jdom very easy to master. Jdom is much easier to process XML than DOM, and its functionality is more powerful than using sax.
Jdom's internal logical structure is essentially the same as Dom's, for example, document node types with documents, Element, comment, each of which must have a document node and the root node of the node tree. The root node can have child nodes or leaf nodes such as comment, text, and so on. Each node type in the Jdom document corresponds to no element in the well-formed XML text. This provides an operational basis for using JDOM to transform the database to XML documents.
Advantages of Jdom:
It is clear from Jdom's documentation that the advantages of applying jdom are obvious. The Jdom document declares the following "Jdom quotes 20/80 principles, even with 20% of the energy to solve the 80% java/xml problem".
Jdom is developed in Java and serviced by Java, which follows the specification and class library of Java code;
In many programming languages, Java is an excellent platform for using XML, and XML is an excellent data representation method for Java applications. The JDOM API is a pure Java API that is easier for Java developers to get started with;
The JDOM API is more intuitive than the methods provided by DOM and simplifies interaction with XML. Faster than using the DOM.
Org.jdom is the JDOM Toolkit for Java API operations.
In Org.jdom, document, Element, Comment, DocType, Attribute, text, and other JAVA classes are provided, which are necessary to access and manipulate jdom documents. We can use these classes to create, traverse, and modify jdom documents.
In Org.jdom.output, Domoutputter and Xmloutputter are provided to handle the DOM tree form of the jdom tree, the output of XML documents, printing, and so on.
Second, the Environment configuration
On my WINDOWS2000 system platform, we use TOMCAT4.1.18 and jdk1.4.0_02 as the development and test platform.
Add the following two environment settings through the advanced attribute of my Computer classpath set 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 into Tomcat's lib directory.
Iii. acquisition and installation of Jdom
Since Jdom is not currently included in the Sun's JDK (I think jdom will inevitably become part of the Sun JDK in the near future), we must manually download and set up the JDOM environment.
The latest version of Jdom can be downloaded at http://www.jdom.org. Here is the download of Jdom beta8. After downloading the jdom-b8.zip, the Jdom jar file is the file Jdom.jar in the build directory, and copies the above files to the Jre/lib/ext directory under the j2sdk1.4.0_02 directory.
Iv. using Jdom to realize the transformation of SQL SERVER database into XML
1, database, table creation
Here is an example of changing customer information-"customer base information". We put the data in a Microsoft SQL SERVER 2000 database and then dynamically generate XML instance documents using JSP and Jdom technology.
We created a datasheet--custom in advance of Microsoft SQL Server database CUSTOM, and its data structure is shown in the following table:
Field name
Comments
Type
is empty
Name
Name
varchar (12)
Not NULL
Id
Id
Int
Not NULL
Company
Company
varchar (30)
Not NULL
Email
TEL
varchar (15)
Not NULL
Enter the following several records into the database:
Name
Id
Company
Email
Liu Shi
001
Company 1
Liu@yantai.com
Jiang Shi
002
Company 2
Jiang@yantai.net
2. Data source Settings
The data source (ODBC source) is actually the source that defines the data. The data source is set up by: [Start]-〉[Settings]-〉[Control Panel]-〉[Admin tool]-〉[Data Source (ODBC)]-〉[system DSN]->[ADD]->[SQL Server], configure the server name (custom server) , database name (custom), data source name (set here as Db_custom), user name (lgz), user password (empty), data source is configured.
3, the Code writing
Let's start by writing JSP code transformation, which will dynamically generate XML structures by invoking Jdom and then using JDBC to access the SQL Server database to populate the XML content dynamically.
<% @page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<HTML>
<HEAD>
<TITLE> Conversion of database to XML using Jdom </TITLE>
</HEAD>
<BODY>
<% @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";
Drivers for loading JDBC ODBC
String user= "Lgz";
String password= "Lgz";
Connection conn= drivermanager.getconnection (Url,user,password); Connecting to a database
Statement stmt=conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);
Create a statement
String sql= "SELECT * from client"; To define the SQL statement for a query
ResultSet rs=stmt.executequery (SQL); Execute Query
Document Document=new Documents (new Element ("Contact list");//Create Documentation
ResultSetMetaData RSMD = Rs.getmetadata (); Get field Name
int numberofcolumns = Rsmd.getcolumncount (); Get number of fields
int i=0;
while (Rs.next ()) {//Remove query Results
Element element0=new Element ("contact"); Creating an element to generate the Jdom tree
Document.getrootelement (). Addcontent (ELEMENT0);
for (I=1; i<=numberofcolumns;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 Generation completed.") ");%>
<a href= "file:///d|/data.xml" > Click to open the resulting XML document </a>
</BODY>
</HTML>
, the operation of the program
Running the JSP through the browser's 8080 port will produce the expected XML file.
V. Application analysis
As the standard of data exchange, XML has been used more and more widely. This article illustrates the basic methods of database conversion to XML, which makes it possible to exchange data between heterogeneous platforms and various formats. When we can convert raw data into XML format, we can express it in rich HTML format. Here are two examples of applications:
1. XML Application Server:
XML application servers are actually Web application servers that support XML, which are typically template-driven, using SQL statements to extract data and dynamically build XML documents in a script language.
2, based on XML desktop applications
We know that through XSL we can submit the same data to end users in different data formats, an XSL file describes how the data is displayed, and 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
There are two main advantages to this approach, first of all, you can operate the data in a platform and language independent way, and secondly, you can implement different view representations of the same data without the need for programming.