Partager via


XMLAdapter Class

XMLAdapter is a "two-way" class. You can import XML and its contained schema to create tables, or you can create and generate XML from tables.

An XMLAdapter object stores object references to XML Schema and Document Object Model (DOM) nodes. It does not store actual XML schema information or content. An XMLAdapter object contains a Tables collection that contains one or more XMLTable objects and describes XML as Visual FoxPro cursors along with any relational information. Each XMLTable object can also contain a child XMLTable, and, at most, a Fields collection with one or more XMLField objects.

Note

The XMLAdapter class requires Microsoft XML Core Services (MSXML) 4.0 Service Pack 1 (SP1) or later.

Using the XMLAdapter class, you can perform the following tasks:

  • Interpret the following schema formats when you use the XMLAdapter LoadXML and Attach methods.:

    • W3C XML Schemas (XS), W3C XML Schemas (XS) as used with .NET DataSets and SQL-XML

    • Microsoft XML Data Reduced Schema (XDR) as implemented by SQL-XML

    • ADO Recordset Schemas

      Note

      The XMLAdapter LoadXML method might not correctly interpret other XML schema formats as produced by other products.

  • Retrieve XML and its associated schema from an XML source. XMLAdapter factors the contained schema into an XMLTable and its corresponding XMLField objects using the XMLAdapter LoadXML or Attach methods.

  • Create a cursor based on the schema and populate it with data represented by an XMLTable object when you use the XMLTable ToCursor method.

  • Represent changes from a .NET DiffGram to local or remote tables when you use the XMLTable ApplyDiffgram method.

  • Add one or more XMLTable objects to the XMLAdapter Tables collection or specify that the XMLAdapter LoadXML, Attach, or AddTableSchema methods perform this for you.

  • Use the XMLAdapter ToXML method to generate an XML document in W3C XML Schema format from one or more cursors or tables referenced by the XMLAdapter object.

XMLAdapter

Remarks

If you use an XMLAdapter object to convert XDR schema to XSD schema, you must change the XMLAdapter XMLNamespace property; otherwise, the XML parser cannot reload the XML. The following example illustrates this scenario:

TEXT TO cXML NOSHOW
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
   xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
   xmlns:rs='urn:schemas-microsoft-com:rowset'
   xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
   <s:ElementType name='row' content='eltOnly'>
      <s:AttributeType name='xmlfield' rs:number='1' 
         rs:writeunknown='true' rs:nullable='true'>
   <s:datatype dt:type='number' rs:dbtype='currency' dt:maxLength='8'
      rs:scale='4' rs:precision='6' />
</s:AttributeType>
      <s:extends type='rs:rowbase'/>
   </s:ElementType>
</s:Schema>
<rs:data>
   <z:row xmlfield='12.12'/>
</rs:data>
</xml>
ENDTEXT

CLOSE DATABASES ALL
CLEAR

LOCAL oXMLAdapter as XMLAdapter
oXMLAdapter = NEWOBJECT('XMLAdapter') 
oXMLadapter.LoadXML(cXML) 
IF oXMLAdapter.Tables.Item(1).Fields.Item(1).DataType <> "Y" THEN
   ? 'Failed'
ELSE
   oXMLAdapter.Tables.Item(1).ToCursor()
   oXMLAdapter.XMLNamespace=""
   oXMLAdapter.ReleaseXML(.F.) 
   oXMLAdapter.XMLSchemaLocation='c:\myxmlfile.xsd'
   oXMLAdapter.ToXML('c:\myxmlfile.xml',,.T.) 
   oXMLadapter2 = NEWOBJECT('xmladapter') 
   oXMLAdapter2.XMLSchemaLocation='c:\myxmlfile.xsd'
   oXMLAdapter2.LoadXML('c:\myxmlfile.xml',.T.,.T.) 
ENDIF

When the schema is missing or not available, you can specify a schema that Visual FoxPro can use by setting the XMLAdapter XMLSchemaLocation property. You must specify this property before calling LoadXML. When Visual FoxPro executes LoadXML, Visual FoxPro looks for the schema in the following order:

  • Inline schema

  • External schema as specified in the XML document

  • Schema as specified by the XMLSchemaLocation property

The following code creates an XMLAdapter object with a Tables collection that remains empty until you load valid XML into the XMLAdapter object:

oMyAdapter=CREATEOBJECT("xmladapter")

The XML and XML schema data you retrieve using the XMLAdapter LoadXML method remains in memory until they are replaced by a subsequent call to LoadXML or when they are specifically released when you call the XMLAdapter ReleaseXML method.

Nested Tables   The following table describes how XMLAdapter treats nested tables as either individual tables or as a whole table depending on the XML data source.

XML data source

Description

ADO.NET DataSet

XMLAdapter considers each nested table as a separate table, which you can convert to a Visual FoxPro cursor. When the XML schema is analyzed, the XMLAdapter LoadXML and Attach methods create an individual XMLTable object for each nested table and adds it to the Tables collection. You can then use the XMLTable ToCursor, ChangesToCursor, and ApplyDiffgram methods to obtain data for each table.

SQL XML

XMLAdapter considers each nested table as inseparable from the parent table. When data is retrieved, the final result of a join operation between those tables is represented as a single result. If multiple levels of nesting exist, such as a child table containing a child table and so on, this single result contains data from a join operation between all tables in the hierarchy.

The XMLAdapter LoadXML and Attach methods create XMLTable objects for all tables. However, only the XMLTable object for the top-level parent table is added to the Tables collection. This XMLTable object represents the parent table and provides access to the single and final result from the join operation through the XMLTable ToCursor method. XMLTable objects for child tables are linked to their parents through the ChildTable and ParentTable properties. However, no way exists to obtain data from individual tables in the chain.

See Also

Reference

XMLAdapter Object Properties, Methods, and Events

Tables Collection (XMLAdapter)

XMLTable Class

Fields Collection (XMLTable)

XMLField Class

LoadXML Method

Attach Method (Visual FoxPro)

ToCursor Method

ReleaseXML Method

ApplyDiffgram Method

XMLSchemaLocation Property

Other Resources

Objects, Collections, and Classes