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 2

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

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:\XMLAdapter\CustomerXML.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.



Comment and Contribute

 

 

 

 

 


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

 

 

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