The Document Object Model

 <  Day Day Up  >  

The programming object that controls the way we use XML is based on the XML Document Object Model, implemented by Microsoft as a series of DLLs called MSXML.DLL, MSXML2.DLL, and MSXML3.DLL. They are registered as COM objects with the names MSXML.DOMDocument, MSXML2.DOMDocument, and MSXML3.DOMDocument respectively. To use them, use CreateObject in FoxPro, or add a reference and create an object from them in Visual Basic .NET:

 

 LOCAL loDOM AS "MSXML2.DOMDocument" loDOM = CREATEOBJECT ("MSXML2.DOMDocument.4.0" ) 

This enables IntelliSense to expose the many properties and methods of the DOM, as shown in Figure 7.4.

Figure 7.4. The XML Document Object Model exposed via IntelliSense.
graphics/07fig04.jpg

The Load method can read the file directly. It can also read a URL. The result is stored in the object's XML property. You can use MessageBox to display loXML.XML if you want to see whether it worked. Note that I've occasionally had problems if I didn't turn asynchronous processing off by including the line

 

 loDOM.Async = .F. 

Errors encountered by the DOM will not be trapped either by FoxPro's ON ERROR or by TRY...ENDTRY . However, the DOM object also reports any errors it encounters. The program shown in Listing 7.8 will report an error in the named XML file, which is a copy of Simple.XML with a few extra characters in one of the attributes.

Listing 7.8. Trapping XML Errors Using the DOM
 LOCAL loDOM AS "MSXML2.DOMDocument.4.0" loDOM = CREATEOBJECT ("MSXML2.DOMDocument.4.0" ) loDOM.Async = .F. loDOM.Load ("D:93SAMS\Chapter7Code\XMLWithErrors.xml") IF loDOM.ParseError.ErrorCode <> 0   Texto = [There was an error in the document:]          + CHR(13) ;     + [    Line: ] + TRANSFORM(loDOM.parseError.Line)    + CHR(13) ;     + [Position: ] + TRANSFORM(loDOM.parseError.LinePos) + CHR(13) ;     + [  Reason: ] + TRANSFORM(loDOM.parseError.Reason)   MessageBox ( Texto, 16, [Error leyendo XML] ) ENDIF loDOM = NULL RELEASE loDOM 

You can also build XML directly in code, as shown in Listing 7.9.

Listing 7.9. Building XML in Code
 USE CUSTOMERS LOCATE FOR CustomerID = [AROUT] PUBLIC lcXML AS String lcXML = [] TEXT TO lcXML NOSHOW <clients>  <client>   <companyname><<CompanyName>></companyname>   <contactname><<ContactName>></contactname>  </client> </clients> ENDTEXT MESSAGEBOX( lcXML ) LOCAL loXML AS MSXML2.DOMDocument loXML = CREATEOBJECT ( "MSXML2.DOMDocument" ) loXML.LoadXML ( lcXML ) MESSAGEBOX( loXML.xml ) 

Or, you can create elements one at a time, as shown in Listing 7.10.

Listing 7.10. Building XML in Code by Elements
 LOCAL loRoot    AS "MXSML2.IXMLDOMElement" LOCAL loElement AS "MXSML2.IXMLDOMElement" LOCAL loText    AS "MSXML2.IXMLDOMText" loRoot = loDOM.documentElement loElement = loDOM.createElement          ( "client"    ) loRoot.appendChild                       ( loElement    ) loElement = loDOM.createElement          ( "name"    ) loText    = loDOM.createTextNode         ( "Juan"    ) loElement.AppendChild                    ( loText    ) loRoot.ChildNodes.item(1).AppendChild    ( loElement    ) loElement = loDOM.createElement          ( "lastname"    ) loText   = loDOM.createTextNode          ( "Perez"    ) loElement.AppendChild                    ( loTexto    ) loRoot.ChildNodes.item(1).AppendChild    ( loElemento    ) loDOM.Save ( "OneClient.XML" ) 

The code to do this in Visual Basic .NET is virtually identical. You just need to change the LOCAL declarations to DIM.

 <  Day Day Up  >  


Visual Fox Pro to Visual Basic.NET
Visual FoxPro to Visual Basic .NET
ISBN: 0672326493
EAN: 2147483647
Year: 2004
Pages: 130
Authors: Les Pinter

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net