• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

MSXML Examples

Started by José Roca, September 02, 2011, 12:12:29 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
What is XML DOM?

The Document Object Model (DOM) as implemented in MSXML provides a programmatic representation of XML documents, fragments, nodes, or node-sets. It also provides an application programming interface for working with XML data. As an XML representation, it conforms to the W3C DOM specification.

The following code fragments outline the basic process of programming with XML DOM using the direct interfaces support of the PowerBASIC compilers.



  • To work with XML data programmatically, you first create an XML DOM object. The following code fragment is an example.

    pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"


  • Next, you can load XML data from a file into the DOM object, as follows:

    pXmlDoc.load "file.xml"


  • Alternatively, you can load data from an XML stream that originated from another application, or that was created dynamically:

    strXml = "<a><a1>1</a1><a2>2</a2></a>"
    pXmlDoc.loadXML strXml



  • To navigate to a node in the XML document, you can specify an XPath expression in the call to one of several methods on the DOM instance, for example:

    pNode = pXmlDoc.selectSingleNode("//a2")


  • To insert a new element into a DOM object, you set properties and call methods on the object, and possibly on its child objects. For example, the following code fragment appends an empty <a3> element as a new child of the document element, <a>:

    pRootNode = pXmlDoc.documentElement
    pNewElement = pXmlDoc.createElement("a3")
    pNewChildNode = pRootNode.appendChild(pNewElement)




  • To persist a DOM object, call the save method on the object:

    pXmlDoc.save "new.xml"



  • To perform XSL Transformations (XSLT) on an XML document, you can create another DOM object to hold the XSLT style sheet, and then call the transformNode method on the DOM object for the XML document:

    pXslt = NEWCOM Msxml2.DOMDocument.6.0"
    pXmlDoc.load "transform.xsl"
    pXmlDoc.transformnode(pXslt)




These are just a few simple examples to show you how DOM can be used to work with an XML document.

José Roca


' ========================================================================================
' Demonstrates the use of the add method.
' The following example attaches a schema to an XML document.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pSchemaCache AS IXMLDOMSchemaCollection

   pXmlDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.4.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
   pSchemaCache = NEWCOM "Msxml2.XMLSchemaCache.4.0"
   IF ISNOTHING(pSchemaCache) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.validateOnParse = %VARIANT_FALSE
      pSchemaCache.add "x-schema:books", "collection.xdr"
      pXmlDoc.putref_schemas = pSchemaCache
      ' The document will load only if a valid schema is attached to the xml file.
      pXmlDoc.load "collection.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         AfxShowMsg pXmlDoc.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the addCollection method.
' Note  MSXML 6.0 has removed support for XDR schemas, whereas XDR is supported in
' MSXML 3.0 AND MSXML 4.0. If this method is called with an XDR schema, the call will fail.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pSchemaCache AS IXMLDOMSchemaCollection
   LOCAL pSchemaCache2 AS IXMLDOMSchemaCollection

   ' XDR schemas are only supported in MSXML 3.0 AND MSXML 4.0.
   pXmlDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.4.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
   pSchemaCache = NEWCOM "Msxml2.XMLSchemaCache.4.0"
   IF ISNOTHING(pSchemaCache) THEN EXIT FUNCTION
   pSchemaCache2 = NEWCOM "Msxml2.XMLSchemaCache.4.0"
   IF ISNOTHING(pSchemaCache2) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.validateOnParse = %VARIANT_TRUE
      pSchemaCache.add "x-schema:books", "collection.xsd"
      pSchemaCache2.addCollection pSchemaCache
      pSchemaCache2.add "x-schema:books", "NewBooks.xsd"
      pXmlDoc.putref_schemas = pSchemaCache2
      ' The document will load only if a valid schema is attached to the xml file.
      pXmlDoc.load "collection.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         AfxShowMsg pXmlDoc.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the addObject method.
' The following example passes an object to the style sheet.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pXslDoc AS IXMLDOMDocument
   LOCAL pXslTempl AS IXSLTemplate
   LOCAL pXslProc AS IXSLProcessor
   LOCAL pXmlElement AS IXMLDOMElement
   LOCAL vOutput AS VARIANT

   pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
   IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
   pXmlDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
   pXslTempl = NEWCOM "Msxml2.XSLTemplate.6.0"
   IF ISNOTHING(pXslTempl) THEN EXIT FUNCTION

   TRY
      pXslDoc.load("sampleXSLWithObject.xml")
      IF pXslDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pXmlElement = pXslDoc.documentElement
         pXslTempl.putref_stylesheet = pXmlElement
         pXslProc = pXslTempl.createProcessor
         pXmlDoc.loadXML "<level>Twelve</level>"
         pXslProc.input = pXmlDoc
         pXslProc.addObject pXmlDoc, "urn:my-object"
         pXslProc.transform
         vOutput = pXslProc.output
         AfxShowMsg VARIANT$$(vOutput)
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the addParameter method.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pXslt AS IXSLTemplate
   LOCAL pXslDoc AS IXMLDOMDocument
   LOCAL pXslProc AS IXSLProcessor
   LOCAL vOutput AS VARIANT

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
   pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
   IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
   pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
   IF ISNOTHING(pXslt) THEN EXIT FUNCTION

   TRY
      pXslDoc.async = %FALSE
      pXslDoc.load "sample.xsl"
      pXslt.putref_stylesheet = pXslDoc
      pXmlDoc.async = %FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pXslProc = pXslt.createProcessor
         pXslProc.input = pXmlDoc
         pXslProc.addParameter "param1", "Hello"
         pXslProc.transform
         vOutput = pXslProc.output
         AfxShowMsg VARIANT$$(vOutput)
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca

#5

' ========================================================================================
' Demonstrates the use of the allErrors property.
' The example uses two resource files, books.xml and books.xsd. The first is an XML data
' file, and the second is the XML Schema for the XML data. The XML document has two
' invalid <book> elements: <book id="bk002"> and <book id="bk003">. The sample application
' uses several methods and properties of the IXMLDOMParseError2 interface to examine the
' resulting parse errors.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXMLDoc     AS IXMLDOMDocument3
   LOCAL pSCache     AS IXMLDOMSchemaCollection
   LOCAL pEItem      AS IXMLDOMParseError2
   LOCAL pError      AS IXMLDOMParseError2
   LOCAL pErrors     AS IXMLDOMParseErrorCollection
   LOCAL bstrMsg     AS WSTRING
   LOCAL bstrErrors  AS WSTRING
   LOCAL i           AS LONG

   ' Create an instance of XML DOM
   pXMLDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXMLDoc) THEN
      AfxShowMsg "Failed to create an instance on an XML DOM"
      EXIT FUNCTION
   END IF

   ' Create an instance of schema caché
   pSCache = NEWCOM "Msxml2.XMLSchemaCache.6.0"
   IF ISNOTHING(pSCache) THEN
      AfxShowMsg "Cannot instantiate XMLSchemaCache60"
      EXIT FUNCTION
   END IF

   ' Add "urn:books" from "books.xsd" to schema caché
   pSCache.add "urn:books", "books.xsd"
   IF OBJRESULT THEN
      AfxShowMsg "Cannot add 'urn:books' to schema caché. Error &H" & HEX$(OBJRESULT)
      EXIT FUNCTION
   END IF
   ' Set the reference
   pXMLDoc.putref_schemas = pSCache

   ' Set the MultipleErrorMessages property
   pXMLDoc.async = %VARIANT_FALSE
   pXMLDoc.validateOnParse = %VARIANT_FALSE
   pXMLdoc.setProperty "MultipleErrorMessages", %VARIANT_TRUE
   IF OBJRESULT THEN
      AfxShowMsg "Failed to enable mulitple validation errors"
      EXIT FUNCTION
   END IF

   ' Load books.xml
   IF pXMLDoc.load("books.xml") <> %VARIANT_TRUE THEN
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "Failed to load DOM from books.xml" & $CRLF & pXmlDoc.parseError.reason
         EXIT FUNCTION
      END IF
   END IF

   ' Validate the entire DOM object
   pError = pXMLDoc.validate
   IF pError.errorCode <> 0 THEN
      bstrMsg = "Error as returned from validate():" & $CRLF & _
               "Error code: " & FORMAT$(pError.errorCode) & $CRLF & _
               "Error reason: " & pError.reason & $CRLF & _
               "Error location: " & pError.errorXPath & $CRLF
      pErrors = pError.allErrors
      IF ISOBJECT(pErrors) THEN
         bstrErrors = "Errors count: " & FORMAT$(pErrors.length) & $CRLF & _
                     "Error items from the allErrors collection: " & $CRLF
         FOR i = 0 TO pErrors.length - 1
            pEitem = pErrors.item(i)
            IF ISOBJECT(pEitem) THEN
               bstrErrors = bstrErrors & "Error item: " & FORMAT$(i) & $CRLF & _
                           "reason: " & pEitem.reason & $CRLF & _
                           "location: " & pEitem.errorXPath
               pEItem  = NOTHING
            END IF
         NEXT
         pErrors = NOTHING
      END IF
      AfxShowMsg bstrMsg & bstrErrors
   END IF

END FUNCTION
' ========================================================================================


José Roca


' =========================================================================================
' Demonstrates the use of the appendChild method.
' =========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' =========================================================================================
' Main
' =========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pRootNode AS IXMLDOMElement
   LOCAL pNewNode AS IXMLDOMNode
   LOCAL xmlString AS STRING

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %FALSE
      pXmlDoc.load "appendChild.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pRootNode = pXmlDoc.documentElement
         xmlString = pRootNode.xml
         AfxShowMsg "Before appendChild: " & $CRLF & xmlString
         pNewNode = pXmlDoc.createNode(%NODE_ELEMENT, "newChild", "")
         pRootNode.appendChild pNewNode
         xmlString = pRootNode.xml
         AfxShowMsg "After appendChild: " & $CRLF & xmlString
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' =========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the appendData method.
' The following example creates an IXMLDOMComment object and uses the appendData method to
' add text to the string.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pComment AS IXMLDOMComment

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.loadXML "<root></root>"
      pComment = pXmlDoc.createComment("Hello...")
      pComment.appendData " ... World!"
      AfxShowMsg pComment.data
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================



' ========================================================================================
' Demonstrates the use of the appendData method.
' The following example creates an IXMLDOMComment object and uses the appendData method to
' add text to the string.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument3
   LOCAL pComment AS IXMLDOMComment

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.resolveExternals = %VARIANT_TRUE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg pXmlDoc.parseError.reason
      ELSE
         pComment = pXmlDoc.createComment("Hello World!")
         pComment.appendData "Ellohay Orldway!"
         AfxShowMsg pComment.data
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the async property.
' The following example sets the async property of a DOMDocument object to false before
' loading books.xml.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   pXmlDoc.async = %VARIANT_FALSE
   pXmlDoc.load "books.xml"
   IF pXmlDoc.parseError.errorCode THEN
      AfxShowMsg "You have error " & pXmlDoc.parseError.reason
   ELSE
      AfxShowMsg pXmlDoc.xml
   END IF

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the attributes property.
' The following example creates an IXMLDOMNamedNodeMap object from a document's attributes
' property, and then displays the number of nodes in the object.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   pXmlDoc.async = %VARIANT_FALSE
   pXmlDoc.load "books.xml"
   IF pXmlDoc.parseError.errorCode THEN
      AfxShowMsg "You have error " & pXmlDoc.parseError.reason
   ELSE
      pNamedNodeMap = pXmlDoc.documentElement.firstChild.attributes
      AfxShowMsg "Length = " & FORMAT$(pNamedNodeMap.length)
   END IF

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the baseName property.
' The following example displays the value of a node's baseName property.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL MyStr AS STRING

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         MyStr = pXMLDoc.documentElement.childNodes.Item(1).baseName
         AfxShowMsg MyStr
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the childNodes property.
' The following example uses the childNodes property (collection) to return an
' IXMLDOMNodeList, and then iterates through the collection, displaying the value of each
' item's xml property.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pRootNode AS IXMLDOMElement
   LOCAL pNodeList AS IXMLDOMNodeList
   LOCAL pCurrNode AS IXMLDOMNode
   LOCAL i AS LONG

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pRootNode = pXmlDoc.documentElement
         pNodeList = pRootNode.childNodes
         FOR i = 0 TO pNodeList.length - 1
            pCurrNode = pNodeList.item(i)
            AfxShowMsg pCurrNode.xml
            pCurrNode = NOTHING
         NEXT
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the clone method.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pXPath AS IXMLDOMSelection
   LOCAL pXPath2 AS IXMLDOMSelection
   LOCAL pTemp1 AS IXMLDOMNode
   LOCAL pTemp2 AS IXMLDOMNode

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmLDoc.loadXML "<root><elem1>Hello</elem1><elem2>World!</elem2></root>"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         ' Create an XMLDOMSelection object from selected nodes.
         pXPath = pXmlDoc.selectNodes("root/elem1")
         ' Cache the XPath expression and context.
         pXPath.expr = "root/elem1"
         pXPath.putref_context = pXmlDoc
         ' Clone the XMLDOMSelection object.
         pXPath2 = pXPath.clone
         pTemp1 = pXpath.peekNode    ' temp1 == <elem1/>
         AfxShowMsg pTemp1.xml
         pTemp2 = pXPath2.peekNode   ' temp2 == <elem1/>
         AfxShowMsg pTemp2.xml
         ' Note that position and context are maintained.
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the cloneNode method.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pRootNode AS IXMLDOMElement
   LOCAL pCurrNode AS IXMLDOMNode
   LOCAL pMyNewNode AS IXMLDOMNode

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pRootNode = pXmlDoc.documentElement
         pCurrNode = pRootNode.childNodes.item(1)
         pMyNewNode = pCurrNode.cloneNode(%VARIANT_TRUE)
         pRootNode.appendChild pMyNewNode
         AfxShowMsg pXmlDoc.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the context property.
' The following example shows what is contained in the context of a selection after a
' query is executed. It also shows that the selection is reset when this property is
' changed.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pSelection AS IXMLDOMSelection

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.loadXML "<Customer>Microsoft</Customer>"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pXmlDoc.setProperty "SelectionLanguage", "XPath"
         pSelection = pXmlDoc.selectNodes("//Customer")
         AfxShowMsg pSelection.context.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================



' ========================================================================================
' Demonstrates the use of the context property.
' The following example shows what is contained in the context property after selectNodes
' is executed.
' ========================================================================================

' SED_PBCC - Use the console compiler
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pSelection AS IXMLDOMSelection

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "samplexml.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pSelection = pXmlDoc.selectNodes("*/BOOK[TITLE='Lover Birds']")
         AfxShowMsg pSelection.context.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================