Application of JSP to implement Jdom processing database to XML transformation

Source: Internet
Author: User
Tags add date format empty odbc sql string stmt
dom|js|xml| Data | database | conversion


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_02libdt.jar;c:j2sdk1.4.0_02lib Ools.jar; ". Path is set to ".; c:j2sdk1.4.0_02in; ".



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's an example of converting customer information?? " Customer basic 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 annotation type is empty name varchar (a) not nullididintnot nullcompany company varchar () not Nullemailtelvarchar (a) not Null enter the following records into the database: Nameidcompanyemail Liu's 001 company 1liu@yantai.com Chiang's 002 company 2jiang@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 (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〉 Convert Database to XML with Jdom @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); Connection Database Statement stmt=conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable); Create a statementstring sql= "select * from client"; The SQL statement that defines the query resultset rs=stmt.executequery (SQL); Execute the query document DOCUMENT=NEW document (New Element ("Contact list");//Create document ResultSetMetaData RSMD = Rs.getmetadata (); Gets the field name int numberofcolumns = Rsmd.getcolumncount (); Get the number of fields int i=0;while (Rs.next ()) {//Remove the result of the query element element0=new element ("Contacts");//create Element Generation 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 (); Closes the result set stmt.close (); Close Statementconn.close (); Close connection Xmloutputter OUTP = new Xmloutputter (); Outp.output (document, New FileOutputStream ("D:\data.xml")); Output XML document OUT.PRINT ("XML document Generation complete!") %〉〈a href= "File:///d|/data.xml" click Open the resulting XML document 〈/a〉〈/body〉〈/html〉



4, 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.




Related Article

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.