This topic assumes a strong understanding of Apache XML Beans. For additional information about XML Bean see the Apache XML Beans Site http://xmlbeans.apache.org/.
The following topic explains how to return XMLBean types from custom Jdbc controls.
An XMLBean is essentially an XML document with a Java API attached to it. The API is used for parsing and manipulating the data in the XML document.
The data can be accessed and manipulated using the XMLBean's API. For example, assume that custBean represents the XML document above. The following Java code extracts the Fred Williams from the document.String name = custBean.getXCustomer().getXCustomerRowArray(1).getNAME();
Retrofitting database controls to return XMLBeans rather than RowSets, ResultSets, or Iterators, is a powerful technique because there are few restrictions on where XMLBeans can be imported. This is not the case with ResultSets and Iterators, which cannot be passed directly to web-tier classes (web services and page flows). Also, data in XMLBean form is very easy to manipulate because there is a rich API attached to the XMLBean.
Creating a Schema
The first step in using XMLBean classes is creating a schema from which the XMLBean classes can be generated. The schema you create for a database control must be capable of modeling the sorts of data returned from the database.
If you write your own schema, at a minimum, the schema's elements should have the same names as the fields in the database, which allows data returned from the database to be automatically mapped into the XMLBean.
When the XSD file is compiled, XMLBean types are generated that can be returned by the methods in the database control.
Editing Schemas to Create New "Document" Types
Note that only one of the generated types is a "Document" XMLBean type: XCustomerDocument. The other types, XCustomerDocument.XCustomer and XCustomerDocument.XCustomer.XCustomerRow, can only be used with reference to the "Document" type. This distinction is especially important because only "Document" types are eligible for direct participation in a business process, or to be passed to a web service. For this reason you may want to edit your schema to include "Document" types corresponding to other types in the Schema, especially if you have a very large schema with many nested types defined in terms of a single "Document" type.
To generate a new Document type for some element, move that element so that it becomes a top-level element in the schema. In the following example, the XCustomerRow element has been moved to the top-level of the schema: its original position has been replaced with a reference element:
There are now two top-level elements, XCustomer and XCustomerRow, which compile into two corresponding "Document" types: XCustomerDocument and XCustomerRowDocument.
Returning a XMLBean Types from Control Methods
Once you have generated XMLBean types that model the database data, you can import these types into your Jdbc control.
import databaseCustomerDb.XCustomerDocument;
import databaseCustomerDb.XCustomerDocument.XCustomer;
import databaseCustomerDb.XCustomerDocument.Factory;
XMLBean types can be returned from the control's methods.
@SQL(statement="SELECT custid, name, address FROM customer")
public XCustomerDocument findAllCustomersDoc();
The data returned from the query is automatically mapped into the XMLBean because the names of the database fields match the fields of the XMLBean.
Here is the apache beehive url: http://beehive.apache.org/docs/1.0.2/system-controls/jdbc/guide.html#Returning+Apache+XMLBeans+from+a+Jdbc+Control
Comments
Post a Comment