Apply a JSP implementation for the JDOM processing database to XML conversion

xiaoxiao2021-03-18  250

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

Email

Tel

Varchar (15)

NOT NULL

Enter the following records into 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 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"%>

Implement the transformation of databases to XML with JDOM </ Title></p> <p></ HEAD></p> <p><Body></p> <p><% @ page import = "org.jdom. *"%></p> <p><% @ Page Import = "Java. *"%></p> <p><%</p> <p>Class.Forname ("com.microsoft.jdbc.sqlserver.sqlserverdriver). NewInstance ();</p> <p>String Url = "JDBC: Microsoft: SQLServer: //10.40.14.54: 1433; DatabaseName = DB_CUSTOM";</p> <p>/ / Load JDBC ODBC driver</p> <p>String User = "LGZ";</p> <p>String password = "lgz";</p> <p>Connection conn = drivermanager.getConnection (URL, User, Password); / / Connecting Database</p> <p>Statement Stmt = Conn.createStatement (ResultSet.Type_Scroll_Sensitive, ResultSet.concur_Updata);</p> <p>// Create a Statement</p> <p>String SQL = "SELECT * from client"; // Define the SQL statement of the query</p> <p>ResultSet RS = Stmt.executeQuery (SQL); // Execute Query</p> <p>Document Document = New Document (New Element); // Create a document</p> <p>ResultSetMetadata RSMD = rs.getMetadata (); // Get field name int number = rsmd.getColumnCount (); // Get field number</p> <p>INT i = 0;</p> <p>While (rs.next ()) {// Remove the query results</p> <p>Element Element0 = New Element; // Creating an element to generate a JDOM tree</p> <p>Document.getrootElement (). AddContent (Element0);</p> <p>For (i = 1; i <= numberofcolumn; i )</p> <p>{String Date = new string (rs.getstring (i) .GetBytes ("ISO-8859-1"), "GB2312"); // Code Conversion</p> <p>Element Element = New Element (rsmd.getcolumnname (i)). SetText (Date);</p> <p>Element0.addContent (Element);</p> <p>}</p> <p>}</p> <p>rs.close (); // Close result set</p> <p>Stmt.close (); // Close Statement</p> <p>Conn.close (); // Close connection</p> <p>XmloutPutter outp = new xmloutputter ();</p> <p>Outp.output (Document, New FileoutputStream ("D: //Data.xml")); // Output XML document</p> <p>Out.print ("XML document is generated!");</p> <p>%></p> <p><a href="file: ///d|/data.xml"> Click to open the resulting XML document </a></p> <p></ Body></p> <p></ Html></p> <p>4, the operation of the program</p> <p>Run the JSP through the 8080 port of the browser, the expected XML file will be generated.</p> <p>V. Application analysis</p> <p>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:</p> <p>1, XML application server:</p> <p>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.</p> <p>2, XML-based desktop application</p> <p>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</p> <p>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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-129973.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="129973" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.047</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'vHB3PIXwug_2FG5oXg_2F_2BeMeWmD01vNwfnS02SynrlxLJiBtHFAZw4dW9hz6VFQScf328wyjUXseHyfDxiIdENzjQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>