JSP implementation for processing database-to-XML conversion using JDOM _ MySQL

Source: Internet
Author: User
Tags xsl xsl file
I. JDOM introduction we know that DOM is the official W3C standard for expressing XML documents in a way unrelated to the platform and language. DOM and SAXAPI can be used to parse and process XML documents. Here we introduce JDOM as a pure Java API based on tree operations. it should be said that it provides a set of solutions for parsing, creating, processing, and implementing XML. These APIs are better than the JDOM provided by DOM and SAXAPI.

1. Introduction to JDOM

We know that DOM is the official W3C standard for expressing XML documents in a way unrelated to the platform and language. DOM and SAX APIs can be used to parse and process XML documents. Here we introduce JDOM, a pure java api based on tree operations. it should be said that it provides a set of solutions for parsing, creating, processing, and implementing XML. These APIs are more intuitive than the methods provided by DOM and SAX APIs. programmers with JAVA experience will find JDOM very easy to master. JDOM is much easier to process XML than DOM, and it is more powerful than SAX.

The internal logic structure of JDOM is basically the same as that of DOM. for example, JDOM has Document, Element, Comment, and other Document node types. each JDOM Document must have a Document node, and is the root node of the node tree. The root node can have subnodes or leaf nodes such as Comment and Text. Each node type in the JDOM document corresponds to none of the well-formed XML files. This provides an operational basis for us to use JDOM to convert databases to XML documents.

Advantages of JDOM:

The document declaration of JDOM clearly shows the advantages of using JDOM. The JDOM document declares that "JDOM references the 20/80 principle, that is, 20% of the effort is used to solve 80% of JAVA/XML problems ".

● JDOM is developed in JAVA and provides services for JAVA. It follows the JAVA code specifications and class libraries;

● Among the many programming languages, JAVA is an excellent platform for using XML, and XML is an excellent data representation method for JAVA applications. JDOM APIs are pure JAVA APIs that are easier for JAVA developers to use;

● JDOM APIs are more intuitive than the methods provided by DOM and simplify interaction with XML. It is faster than DOM.

Org. jdom is a JDOM toolkit for java api operations.

In org. jdom, JAVA classes such as Document, Element, Comment, DocType, Attribute, and Text are provided. These classes are required to access and operate JDOM documents. We can use these classes to create, Traverse, and modify JDOM documents.

In org. jdom. output, DOMOutputter and XMLOutputter are provided for processing the DOM tree form of the JDOM tree, XML document form output, and printing.

II. environment configuration

On my WINDOWS2000 system platform, Tomcat 4.1.18 and JDK 1.4.0 _ 02 are used as the development and testing platforms.

Use the "advanced" attribute of "my computer" to add the following two environment settings CLASSPATH: ".; c: j2sdk1.4.0 _ 02libdt. jar; c: j2sdk1.4.0 _ 02lib ools. jar ;". Set PATH to ".; c: j2sdk1.4.0 _ 02in ;".

SQL SERVER JDBC driver: put mssqlserver. jar in the LIB directory of TOMCAT.

3. obtain and install JDOM

Currently, JDOM is not included in SUN's JDK (I think JDOM will inevitably become part of SUN's JDK in the near future). we must manually download and set the JDOM environment.

In the http://www.jdom.org, you can download the latest version of JDOM. Here we download JDOM beta8. Download jdom-b8.zip and decompress it. the JDOM jar file is the jdom. jar file under the build directory. copy the above file to the jre/lib/ext directory under the J2SDK1.4.0 _ 02 directory.

4. use JDOM to convert SQL SERVER database to XML

1. create databases and tables

Here is an example of customer information conversion ?? "Basic customer information ". We place the data in the microsoft SQL SERVER 2000 database, and then use JSP and JDOM technology to dynamically generate XML instance documents.

We created a data table CUSTOM in the Microsoft SQL Server database CUSTOM in advance. its data structure is shown in the following table:

Field name
Note
Type
Empty or not

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 records in the database:

Name
ID
Company
Email

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 the defined data. To set the data source, click [start]-> [Settings]-> [control panel]-> [administrative tools]-> [data source (odbc)] -> [System DSN]-> [add]-> [SQL Server], configure the Server name (M Server) and database name (CUSTOM) respectively) data Source Name (DB_CUSTOM here), user name (lgz), user password (null), the data source is configured.

3. coding


The following describes how to compile JSP code conversion. the code dynamically generates an XML structure by calling JDOM, and then dynamically fills XML content by accessing the SQL SERVER database through JDBC.

<% @ Page contentType = "text/html; charset = GBK" %>

Use JDOM to convert database to XML

<% @ 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 the jdbc odbc driver

String user = "lgz ";

String password = "lgz ";

Connection conn = DriverManager. getConnection (url, user, password); // connect to the database

Statement stmt = conn. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );

// Create a statement

String SQL = "select * from client"; // defines the SQL statement to be queried.

ResultSet rs1_stmt.exe cuteQuery (SQL); // execute the query

Document document = new Document (new Element ("Contact List"); // create a Document

ResultSetMetaData rsmd = rs. getMetaData (); // Obtain the field name

Int numberOfColumns = rsmd. getColumnCount (); // Obtain the number of fields

Int I = 0;

While (rs. next () {// Retrieve the query result

Element element0 = new Element ("Contact"); // create an Element to generate a 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 the result set.

Stmt. close (); // close statement

Conn. close (); // close the connection

XMLOutputter outp = new XMLOutputter ();

Outp. output (document, new FileOutputStream ("d: \ data. xml"); // output the XML document

Out. print ("the XML document has been generated! ");

%>

Click to open the generated XML document

4. program running

Run the JSP on port 8080 of the browser to generate the expected xml file.

5. Application Analysis

XML is widely used as a standard for data exchange. This article illustrates the basic methods for converting databases to XML, making it possible to exchange data between heterogeneous platforms and data in various formats. When we can convert the original data into XML format, it can be expressed in a wide array of HTML formats. Two applications are listed here:

1. XML application server:

The XML application server is actually a Web application server that supports XML. they are usually template-driven. they use SQL statements embedded in a script language to extract data and dynamically construct XML documents.

2. XML-based desktop applications

We know that the same data can be submitted to end users in different forms through XSL. an XSL file describes the data display mode, you can connect many XSL with the same XML document to provide different HTML-based representation. In fact, we can build XML-based desktop applications.

This method has two main advantages: first, you can operate data in a platform-independent way, and second, you can implement different views of the same data without programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.