Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

The VFP 8 XMLAdapter Class : Page 4

Visual FoxPro 8.0 introduces a whole new way to work with eXtensible Markup Language (XML). The XMLAdapter class works with hierarchical XML, provides an object-oriented approach to working with XML data, and leverages your familiarity with tables and fields in the way it exposes the XML contents.


advertisement

Working with Diffgrams
The XMLAdapter class supports Diffgrams, which provide an efficient way of transferring data back and forth between applications. Diffgrams are a special type of an XML document that are formatted to only include information for changed data, rather than the entire set of data. Using this format can reduce the size of the XML that needs to be sent between applications.

The developer loads the XML into Visual FoxPro, converts it to a cursor, makes the necessary updates to the data, and converts the data back to a XML document Diffgram. Only the changed data will be returned to the receiver of the XML. This XML document Diffgram can not only be used to apply to another XMLAdapter class in Visual FoxPro but also can be used in other applications that support data in Diffgram format. These XML formats are compatible with ADO.NET for use in Windows .NET Framework applications.

The Windows .NET DataSet is always serialized to XML in Diffgram format when the DataSet is returned to Visual FoxPro. The sample Visual Basic .NET code below returns a Windows .NET DataSet:

   Return MyDataSet
If the DataSet contains changes, they will be flagged via the Diffgram:hasChanges="modified" or Diffgram:hasChanges="inserted" attributes. For "modified" records, the previous values will be in the diffgr:before section. Deleted records appear only in the diffgr:before section and not in the main section of the Diffgram. Either the entire Windows .NET DataSet (all the rows in the original query), or a DataSet that only contains changes, can be returned.

In the case where all rows are returned, unchanged rows will not have a diffgr:hasChanges attribute. To load the DataSet into an XMLAdapter object, you need to use the Attach() method instead of the LoadXML() method since the DataSet returns a XML DOM object. Here is sample Visual FoxPro code:

   adapter = CREATEOBJECT("XMLAdapter")
   * Call a .NET Web Service to return a DataSet
   DataSetadapter.Attach(MyWebService.FetchData())
You can create XML in Diffgram format using the XMLAdapter class. This next example creates a read-write cursor that contains customer information from the new Northwind database (included with Visual FoxPro 8.0) and exports the changed record to a Diffgram XML file:



   cFile = "c:\XMLAdapter\DiffgramXML.xml"
   OPEN DATABASE (_samples+"\northwind\northwind")
   SELECT CustomerID, CompanyName ;
      FROM Customers;
      INTO CURSOR curCustomers READWRITE
   CURSORSETPROP("Buffering", 5)
   LOCATE FOR CustomerID = "AROUT"
   REPLACE CompanyName WITH "Around the Corner"
   adapter = CREATEOBJECT("XMLAdapter")
   adapter.AddTableSchema("curCustomers")
   adapter.IsDiffgram = .T.
   adapter.ToXML(cFile,,.T.,.T.,.T.)
Table buffering is turned on to allow the XMLAdapter object to detect the before and after values for the altered records. A record is then changed to replace the CompanyName field with a different string. The XMLAdapter object is instantiated and the cursor is added to the XMLTable collection. The property IsDiffgram is set to 'true' to allow the exported XML to be formatted for Diffgrams.

Finally, the ToXML() method is called to export the XMLTable to an XML file. Two new parameters are used in this example: The fourth parameter specifies whether to include the diffgr:before section in the Diffgram; and the fifth parameter specifies whether the XML should contain changes only.

Working with XML Web Services
An XML Web service is an application or block of executable code that is hosted on a web server and whose methods are exposed through standard XML protocols to be executed over HTTP. Those methods can be from any platform, from any language on either side. XML Web services are an industry standard for communicating between applications over the Intranet/Internet; and XML is the meta-language that serves as the backbone for these services.

As an example, you might use the XMLAdapter to retrieve data from a Windows .NET Framework application via an XML Web service. The Windows .NET Framework application fetches data using a Visual FoxPro COM object that uses the XMLAdapter class to prepare the data to export in XML Diffgram format. The data is then exposed to other applications, such as other Visual FoxPro applications, mobile devices, .NET Web Forms, and .NET Windows Forms, by using an ASP.NET XML Web service to pass the data in the format of XML to the calling applications.

The Visual FoxPro application uses the XMLAdapter class to accept XML sent by the consumers of the XML Web service and updates the database with the changes. The ApplyDiffgram() method is called on the XMLTable object to apply the changes to the cursor, as named in the Alias property to which it applies. Shown below is the code that accepts an XML Diffgram file and updates the Customers table with the changes:

   OPEN DATABASE (_samples+"\northwind\northwind")
   cFile = "c:\XMLAdapter\DiffgramXML.xml"
   adapter = CREATEOBJECT("XMLAdapter")
   adapter.LoadXML(cFile,.T.)
   adapter.Tables(1).Alias = "Customers"
   USE Customers IN 0
   adapter.Tables(1).ApplyDiffgram()
Working with the New CursorAdapter Class
Using the XMLAdapter class with the new CursorAdapter class provides more control over the XML data versus simply using the CursorAdapter alone. Data types can be changed prior to generating the XML or prior to filling cursors. If the XML file to be imported contains more than one table, the XMLAdapter creates different XMLTable objects.

The XMLAdapter class then offers flexibility as an XML source for CursorAdapter objects by allowing you to specify a valid XMLTable object as the argument for the CursorAdapter.SelectCmd property. The code below demonstrates using the CursorAdapter:

   adapter = CREATEOBJECT("XMLAdapter")
   cFile = "c:\XMLAdapter\CustomerXML.xml"
   adapter.LoadXML(cFile,.T.)
   MyCursorAdapter.SelectCmd="adapter.Tables(1)"
   MyCursorAdapter.CursorFill()
If you are planning to use a CursorAdapter to communicate with an XML Web service via the Soap Toolkit in Visual FoxPro, you will need to use the XMLAdapter class to retrieve the XML from the XML Web service. Do this by using XMLAdapter.Attach() to retrieve a Windows .NET DataSet.

XML provides a universal way of describing, exchanging and moving data over the Intranet/Internet. It is platform-neutral. XML is a basis for Windows .NET technologies. The strategic value of Visual FoxPro 8.0 supporting and embracing XML is that it gives Visual FoxPro the capability to connect to other Visual FoxPro applications and Windows .NET applications, as well as other platforms or systems that support XML—universally integrating business applications.

Aleksey Tsingauz, Developer, Visual FoxPro TeamMicrosoft


Cathi Gero is a consultant, developer, and founder of Prenia Corporation, providing custom software application solutions to businesses since 1987. Her expertise lies in developing .NET business applications, developer consulting in .NET, and database solutions using Visual FoxPro, SQL Server and Crystal Reports. She is a Microsoft MVP and a C.P.A. As a contractor for Microsoft in 2002, Cathi was a member of the FoxPro team working on Visual FoxPro 8.0. She is the Contributing Technical Editor for the book .NET for Visual FoxPro Developers by Hentzenwerke Publishing. Cathi has authored whitepapers for Microsoft and is a speaker at many developer conferences and user groups. Her monthly column, "Cathi Gero's .NET Tips" appears in Universal Thread Magazine and is an example of her involvement in the .NET community. cgero@prenia.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date