he new XMLAdapter class in VFP 8 greatly enhances your ability to work with XML data sources, including hierarchical XML documents, such as .NET DataSets. Cathi explains how this and related new classes open new possibilities for your applications.
Many developers have come to realize the enormous potential of a platform-neutral way to exchange structured data over the Intranet/Internet by using XML. XML makes it possible to integrate your applications with others, even if they’re using platforms and systems completely different than yours. Integrating business applications has become an essential need. XML is the key to this kind of integration.
The XMLAdapter class provides new support for working with XML. One capability of this new class is support for hierarchical XML. This means that an XML file that represents a collection of different and potentially related tables, such as a Windows .NET DataSet, will render into separate Visual FoxPro cursors.
Flexibility and control over data is enhanced by being able to control the schema of the XML that is created, as well as control the data types that the cursor creates from the XML of the schema. This allows you to load in the XML, change the schema, then generate the cursor. In addition, you can take a cursor in memory, control the schema, and then generate the XML in a different format.
Working with XML in Visual FoxPro 8.0 vs. Visual FoxPro 7.0
The XMLTOCURSOR()/CURSORTOXML() functions that were new to Visual FoxPro 7.0 restricted you to working with XML files that contained data for only one table. If more than one table is contained in the XML file, you need to parse through the file manually. In addition, you don’t have the full control of the schema that was contained in the XML file to change the data types before converting the XML to a Visual FoxPro cursor. When the XML was generated from the cursor, you don’t have control of the schema that is generated.
The XMLAdapter class in Visual FoxPro 8.0 greatly enhances XML support to provide you with the ability to work with multiple tables in one XML file, and the compatibility of working with XML from different sources. The schema can be modified to allow you control over how data is converted to a cursor and how the XML is generated from Visual FoxPro cursors.
Introduction to the XMLAdapter, XMLTable, and XMLField Classes
The XMLAdapter class allows you to load XML from an XML source, parses the XML Schema (when it exists), and add one or more XMLTable object(s) to its tables collection. In turn one or more XMLField object(s) are added to the Fields collection of each XMLTable.
|
XMLAdapter class can also create an XML document representing the contained tables and fields that have been populated. XMLAdapter class includes two other child member classes: XMLTable and XMLField. These provide the ability to walk through the schema programmatically and access or set information.
The primary functionality that the XMLAdapter Class provides is to retrieve XML via the LoadXML() method, then parse the XML via the contained XML Schema, as appropriate, into one or more XMLTable objects, which in turn contain XMLField object(s).
The XMLTable class is a collection of all tables contained in the XML and functions to allow you to step through the table to perform procedures on them.
The collection of XMLTable objects describes the XML as a Visual FoxPro cursor or cursors, along with any relational information. The XMLAdapter does not store the actual XML schema or content, but does store object references to them.
The developer may then use the XMLTable.ToCursor() method to produce a cursor that contains the data of all the fields represented by the child member XMLField collection.
The XML and XML Schema data retrieved via the XMLAdapter.LoadXML() method remains in memory until replaced via a subsequent call to LoadXML(), or when it is specifically released by calling the ReleaseXML() method.
The XMLField class is a collection created for each XMLTable and contains all the fields in the table. The developer can iterate through the field objects and make any necessary changes. There are no methods associated with the XMLField class.
Converting XML to VFP Cursors Using the XMLAdapter
Now let’s put the XMLAdapter class to work and see how easy it is to take an XML file and convert it to a Visual FoxPro cursor. We’ll work with an XML file representing Customer data, which contains one table with four records, each containing two fields.
The code to read the XML and create a cursor using the XMLAdapter class is as follows:
cFile = "c:XMLAdapterCustomerXML.xml" adapter = CREATEOBJECT("XMLAdapter") adapter.LoadXML(cFile,.T.) adapter.Tables(1).ToCursor()
First, a reference is made to the XML file. Next, an instance of the XMLAdapter class is created. The LoadXML method is then called, loading the XML from the file into a Document Object Model (DOM) document and attaching it to the XMLAdapter object. The first parameter is the name of the XML file or the string which contains the XML. The second parameter determines if the first parameter represents a file.
Finally, the collection of XMLTable objects is accessed. Since there is only one table contained in the XMLAdapter object, we can directly access the XMLTable. Lastly, the ToCursor method is called to convert the XML to a Visual FoxPro cursor. See Figure 1 for an example of the cursor created.
XMLAdapter Class with SQLXML
The XML generated pulls data from three different tables and the XML data maintains the relationships between these tables. This allows an XMLAdapter object to maintain the parent-child relationships by populating the XMLTable.ParentTable and XMLTable.ChildTable properties. The following Visual FoxPro code uses an XML file to display the relationships between the different XMLTable objects:
When the ToCursor() method is called, only one cursor (called Customers) will be generated, representing the join of all three tables. One note to keep in mind: The XML created above does not generate a schema which sets the maxlength for string fields. Therefore, Visual FoxPro will convert the data type to memos, since that is the default data type for unlimited string lengths. To work around this, you will need to modify the schema before calling the ToCursor() method. Here is the code:
A use for SQLXML is to publish SQL Server data for Intranet and Internet-based applications using the HTTP publishing functionality. The ability to publish data over HTTP allows you to build highly data-centric Web sites. In addition, the XMLAdapter class can retrieve data as XML by simply making an HTTP request. The request can be a select statement or a call to a XML template (see sidebar, “XML Templates”). Working with Windows.NET Framework Return the entire Windows .NET DataSet to the calling application, which returns all rows in DiffGram format with inline schema, having Updates, Inserts, and Deletes indicated. Return Windows .NET DataSet changes only which returns only the rows that have been modified, added, or deleted in Diffgram format with inline schema. Windows .NET DataSet class supports the DataSet.GetXml and GetXmlSchema methods which return XML to a .NET string type.
Windows .NET DataSet class supports the DataSet.WriteXml and WriteXmlSchema methods which write the DataSet as XML with Inline Schema, without Schema, or with Schema separately. A great amount of focus went into making the XMLAdapter class compatible with Windows .NET DataSets. The XMLAdapter class supports hierarchical XML format, which improves Visual FoxPro’s ability to interoperate with XML produced from and written to Windows .NET DataSets. Separate Visual FoxPro cursors will be created for each DataTable contained in the DataSet. Listing 1 shows sample Visual Basic .NET code that retrieves data from three SQL Server tables into one Windows .NET DataSet, then exports the DataSet as XML to an XML file and the schema to a separate XSD file. One note worth mentioning when working with Windows .NET DataSets: You must set the property MissingSchemaAction of the DataAdapter class prior to filling the DataSet; like the following:
This is required to include all length information in the schema. Otherwise, all the .NET Framework string fields will be modified to Memo fields in Visual FoxPro. This is because the memo field is the default type in Visual FoxPro for unlimited length strings. The XMLAdapter class can consume this XML by reading in the data and generating three different cursors. Here is the code:
Three different cursors will be created, as shown in Figure 2. The schema information for this example was created as an external file using Visual Basic .NET. When you have an external file, you need to set the XMLSchemaLocation property of the XMLAdapter object to the XSD file prior to reading in the XML.
|