Setting up the mapping
The goal of this tutorial is to show how to create an XML document out of data from a database using a mapping file. In other words, the map file determines what data is retrieved and how it is ultimately represented in the XML file.
One way to do this is to create a temporary XML document out of the data as it's extracted from the database, then massage that data into the new format according to the map file. The map file determines which data is extracted, the name and structure of the new file, and what data is stored where.
The mapping file contains several pieces of information:
- The original data, in the form of a
dataelement. For maximum flexibility, this is in the form of an SQL statement. In this way, you can use the mapping file to specify that data should be drawn from more than one table.
- The overall structure of the new document. This is
in the form of the
rootelement, which, through attributes, specifies the name of the destination root element and the name of the elements that are to represent database rows.
- The names and contents of data elements. These are
contained in a series of
elements include the name of the new element and any
contentelements. These two elements designate the data that should be added, and, in the case of attributes, what it should be called. For example, if the
descriptionelement should have a
product_numberattribute that represents the
product_idcolumn and the
product_nameas content, the map file would represent it as:
<element name="description"> <attribute name="product_number">product_id</attribute> <content>product_name</content> </element>
The final mapping file is as follows:
<?xml version="1.0"?> <mapping> <data sql="select * from products" /> <root name="pricingInfo" rowName="product"> <element name="description"> <attribute name="product_number">product_id</attribute> <content>product_name</content> </element> <element name="quantity"> <content>lower</content> </element> <element name="size"> <content>size</content> </element> <element name="sizeUnit"> <content>unit</content> </element> <element name="quantityPrice"> <content>unit_price</content> </element> </root> </mapping>