Database xml api (2)

Source: Internet
Author: User
Tags xsl xslt xslt processor

Source: Internet cool work arrangement

Let's take a look at the details of the above Code. JDBCSAXParser includes several overloaded parse () methods. In the following table, the org. xml. sax. Parser interface must implement the parse (InputSource) and parse (String) methods. Other parse () Methods simplify the code and allow overloading of derived classes to change the parsing method.

 

If the parameter is of the JDBCInputSource type, the Parse (InputSource) method calls the parse (JDBCInputSource) method. Otherwise, a SAXException event is generated, indicating that the data source cannot be processed.

When the provided information is insufficient to access the database, the parse (String) method generates a SAXException event.
The Parse (JDBCInputSource) method creates a connection object for the input source, and executes a query to obtain a ResultSet object. You can call the parse (ResultSet) method for this object.

The Parse (ResultSet, String) method executes the core logic of parsing. It traverses each row and field in the result set. Call the methods StartElement () and endElement () for each row in a loop (using the database table ID as the element name parameter ). Similarly, the methods StartElement () and endElement () (using the row ID as the element name parameter) are called during the loop of each field in each row record ). In the preceding two cases, an empty Attribute Table is passed as the second parameter to startElement (). For each field in the access record, the method generateSAXEventForColumn () is called (the Field name and field value are used as parameters ). The value of a single field can be obtained by using the getString90 method on the result set object. Similarly, we need to use a string to characterize the field data and use it in the characters () event.

The parse (String, String) method establishes a JDBCInputSource object by passing the parameters to it, and then uses the parse (JDBCInputSource) method for this object.
Method JDBCSAXParser (protected Mode) provides some special features through overload:

The generateSAXEventForColumn () method generates an event for field data. A null field in a database has different meanings from a zero field (empty. We capture this difference by filtering fields with null values. Another way to express the null value in the database is to use a Binary Attribute (such as isNull ). With this option, a true value is considered as a null value, otherwise it is not.

The GetTableMarker (), getRowMarker (), and getClumnMarker () methods can return the appropriate table, row, and field default values respectively. Derived classes can overload these methods to provide specific identifiers.

The GetTableMarker () method returns a "select * from <tableName>" string. The derived class can provide a different select query string by reloading this method, and implement database-level filtering.

JDBCSAXUtils provides two methods to create a JDBCInputSource object: A property object or a Property object. It does not need to provide a database parameter table to the application through the SAX or DOM programming interface. It allows you to provide an attribute file containing the complete database URL entry, a username and password that can be connected to the database, a JDBC database engine used to establish a connection, and a database table name. The following is a typical property file:

# Portfolio. prop
# JDBCSAXSource property file
URL = jdbc: odbc: db1
User = jw
Password = jw-passwd
Table = portfolio
Driver = sun. jdbc. odbc. JdbcOdbcDriver

Now we have a simple parser that can generate appropriate SAX events for database tables. It can distinguish null values and provide some special identifiers. These functions are sufficient for some applications, and some additional functions are required for some complete solutions, because:

The parser cannot merge the associated information. To solve this problem, you can use Xpointer/Xlink to set information about foreign keys in the table.
A text field in the database may contain the mark (marked-up) information. A database's SAX Parser should be able to parse this type of data and generate appropriate SAX events. If this type of feature is very important to an application, you can reload the generateSAXEventForColumn () method and parse the content of this field to generate additional SAX events.

In a database, a database table contains unordered field sets. Sorting related to field storage is not important. On the other hand, an xml dtd cannot describe an unordered sub-element set.

We can solve this problem in several ways. If we want to convert the database into another XML document, for example, an HTML page, the output results of correct sorting can be created for the XSLT style table customized for it. We can also overload the getSelectorSQLStatement () method to directly provide a list of correctly sorted fields.

Sometimes we want to encapsulate the selected part of a table as an XML document through some queries. If the XML tool can implement such filtering, we can better use the database. The getSelectorSQLStatement () method can be used to reload and return an appropriate select result string.

The parser uses the getString () method to obtain a field value in the string form for the result-set object. This operation is suitable for fields of the text, number, and other types, but not binary data. Some operations cannot be used when text is used to represent binary data. The parser cannot process user-defined data types that can be accepted by SQL3/JDBC2.0.

For the above problems, we can achieve this by reloading the generateSAXEventForCloumn () method and providing an appropriate processing (implementation.

Database-oriented DOM programming interface implementation
To create a DOM tree corresponding to a database table, we can traverse each field in each row and create a tree node for it, or we can use other class libraries, such as Sun's JAXP tool, it can create a DOM tree through a SAX event stream. The latter method is simpler and the code is more concise, because it uses an existing function. To use this method to implement the DOM programming interface, we need an appropriate SAX database parser to make our implementation more convenient.
Integrate the DOM database programming interface with the XQL Processor
Database-oriented DOM programming interface is implemented through the JDBCDOMParser class:

// JDBCDOMParser. java
Package dbxml. dom;
Import java. io. IOException;
Import org. w3c. dom. Document;
Import org. xml. sax. SAXException;
Import com. sun. xml. tree. XmlDocumentBuilder;
Import dbxml. sax .*;
Public class JDBCDOMParser {
Public static Document createDocument (JDBCInputSource inputSource)
Throws SAXException, IOException {
XmlDocumentBuilder documentBuilder = new XmlDocumentBuilder ();
JDBCSAXParser saxParser = new JDBCSAXParser ();
DocumentBuilder. setParser (saxParser );
SaxParser. parse (inputSource );
Return documentBuilder. getDocument ();
}
}

The implementation of the class JDBCDOMParser is relatively simple. It uses the XmlDocumentBuilder class provided by JAXP to build a DOM document from a SAX event stream. JDBCDOMParser has only one method: createDocument (), which requires a JDBC data source as a parameter. This method creates a JDBCSAXParser and can be used to parse an actual XmlDocumentBuilder object. It then releases the parsing object and returns the results generated in the XmlDocumentBuilder object. In actual programming implementation, the XmlDocumentBuilder object responds to the SAX event generated by the JDBCSAXParser object by creating a DOM document method.

· Use the database-oriented SAX Programming Interface

We have read an example of implementing the DOM programming interface through the database-oriented SAX programming interface. Now we need to look at another example of using the SAX programming interface. In this section, we will see how to integrate the SAX database programming interface with XT (An XSLT processor written in Java. Through this integration, we can directly use XSLT style sheets for a virtual XML document stored in the database.

We encapsulate the logic for actually creating a SAX database source, and process it with a given XSLT style table. In the JDBCXSLProcessor class, an output file is generated (using com. jclark. xsl. sax. driver ). The main method includes three parameters: a database feature file, An XSLT style table file, and an output file.
As we will see below, we can use this method to directly generate HTML pages without a temporary XML file for transition. In addition, we will also see how to combine the SAX database programming interface with XT to convert a database into a physical XML document.

(To view the JDBCXSLProcessor. java code, click here .)
Use XSLT style sheets to directly generate HTML pages from databases
Now let's look at a simple style table, which can display a highly standard XML document (based on database tables) in a formatted manner. The database table is formatted as an HTML table. The style sheet createTable. xsl can be used to process any XML document with a table-like structure. The style sheet uses the field tag name as the header title.

(To view the source code of createTable. xsl, click here .)
Use an XSLT style sheet to convert a database into an XML document
Although most XML applications can directly use the SAX or DOM programming interface, we still need to obtain an actual XML document in some cases. For example, for a tool that does not use any xml api, we need a physical XML document. Here, we recommend a method for generating XML documents from databases. In this method, we create An XSLT style table to achieve unified conversion. Using such a style sheet (combined with a SAX Database Programming Interface), we can generate an XML document corresponding to the database table. Here, the author provides a style sheet ?? Identity. xsl ?? It can be used in the current version of XT to achieve unified data conversion.

Identity. xsl
<Xsl: stylesheetversion = "1.0"
Xmlns: xsl = "http://www.w3.org/1999/XSL/Transform">
<Xsl: template match = "@ * | *">
<Xsl: copy>
<Xsl: apply-templates select = "@ * | node ()"/>
</Xsl: copy>
</Xsl: template>
</Xsl: stylesheet>

Note that although an XSLT style sheet can easily implement batch data conversion, this method is not very effective because the logic of a complete style sheet may be very complex, this affects the creation of style sheets and the efficiency of actual data conversion. This problem is especially evident when the database table contains a large number of records. One compromise is to write an application dedicated for batch conversion. Such an application can listen to the SAX event and create XML elements (and the actual data). The generated result XML document corresponds to the database table.

· Use the DOM Database Programming Interface

In most cases, the SAX database programming interface saves more system resources than the DOM programming interface. However, some applications require random access to XML documents, so they need to provide a tree structure similar to DOM to represent the database.

· Integrate the DOM database programming interface with the XQL Processor

XML Query Language (XQL) is a Language used for searching XML documents. Its syntax is similar to that of Xpath. Here we integrate our DOM database parser with the GMD-IPSI's XQL Engine. Through this integration, we can execute SQL-like queries on an XML document that represents a database table.

As an integration example, the author provides a simple encapsulation for querying an XML document based on database tables. JDBCXQLProcessor is used to create an encapsulation-like environment, which receives the query from the customer and outputs The result document. Method PprocessQueries () can operate any document object ?? Not only is it an object created by JDBCDOMParser. It reads the system, executes the query request, and outputs the system query results. The Main () method can create a JDBCInputSource object through its parameters and pass it to JDBCDOMParser to obtain a document object corresponding to the given database table.

(To view the Java code of JDBCXQLProcessor, click here .)
In addition, using XMLWriter and JDBCDOMParser to write a database to XML is a snapshot method. That is, get a Document object from JDBCDOMParser and write it to the target file through XMLWriter. write (Document.

· CONCLUSION

In this article, we discuss how to use XML programming interfaces for databases to characterize information in databases. Through this programming interface, we can avoid converting a database into an actual XML document and avoiding synchronization between the two. We introduced a Java-implemented interface for programming the SAX and DOM databases, and then introduced a method for integrating with XT. We demonstrate how to use this integration to export HTML pages from database tables and convert database tables to XML documents. Finally, we integrate the DOM database programming interface with an XQL processor.

· About the author

Ramnivas Laddad is a Java SUN certified engineer. He has a master's degree in Electronic Engineering Communication Technology and six years of software development experience (about networks, graphic user interfaces, distributed systems, etc ). Rich experience in Object-Oriented Software Systems (five years of C ++ and two years of Java ). Ramnivas is now working at http://www.rti.com/to Design and Research controlshell (a modular programming framework technology for complex real-time monitoring systems ).


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.