• 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


' ========================================================================================
' Demonstrates the use of the documentElement property.
' The following example creates an IXMLDOMElement object and sets it to the root element
' of the document with the documentElement property. It then walks the document tree.
' ========================================================================================

#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.text
            pCurrNode = NOTHING
         NEXT
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


' ========================================================================================
' Demonstrates the use of the docType property.
' The following example creates an IXMLDOMDocumentType object, and then displays the name
' property of the object.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pDocType AS IXMLDOMDocumentType

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

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load("books1.xml")
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pDocType = pXmlDoc.doctype
         IF ISNOTHING(pDocType) THEN
            AfxShowMsg "There is no document type node"
         ELSE
            AfxShowMsg pDocType.name
         END IF
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY


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


José Roca


' ========================================================================================
' Demonstrates the use of the entities property.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pEntity AS IXMLDOMEntity
   LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
   LOCAL pDOMNode AS IXMLDOMNode

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

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "doment1.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pNamedNodeMap = pXmlDoc.docType.entities
         pDOMNode = pNamedNodeMap.nextNode
         pEntity = pDOMNode
         AfxShowMsg pEntity.notationName
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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



' ========================================================================================
' Demonstrates the use of the entities property.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pEntity AS IXMLDOMEntity
   LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
   LOCAL pDOMNode AS IXMLDOMNode

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

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.setProperty "ProhibitDTD", %VARIANT_FALSE
      pXmlDoc.load "doment1.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pNamedNodeMap = pXmlDoc.doctype.entities
         pDOMNode = pNamedNodeMap.nextNode
         pEntity = pDOMNode
         AfxShowMsg pEntity.notationName
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


' ========================================================================================
' Demonstrates the use of the errorCode property.
' The following example attempts to load an XML document. If it encounters a parse error,
' it displays the error code.
' ========================================================================================

#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

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "bad.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 errorParameters method.
' This sample code uses features that were first implemented in MSXML 5.0 for Microsoft
' Office Applications.
' ========================================================================================

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

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

   LOCAL pXmlDoc     AS IXMLDOMDocument3
   LOCAL pSCache     AS IXMLDOMSchemaCollection
   LOCAL pError      AS IXMLDOMParseError2
   LOCAL pNodes      AS IXMLDOMNodeList
   LOCAL pNode       AS IXMLDOMNode
   LOCAL bstrMsg     AS WSTRING
   LOCAL i           AS LONG
   LOCAL x           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
      pError = pXmlDoc.parseError
      AfxShowMsg "Failed to load DOM from books.xml" & $CRLF & pError.reason
      pError = NOTHING
      EXIT FUNCTION
   END IF

   ' Validate the entire DOM object
   pError = pXmlDoc.validate
   bstrMsg = "Validating DOM..." & $CRLF
   IF pError.errorCode <> 0 THEN
      bstrMsg = bstrMsg & "invalid DOM:" & $CRLF & _
                        "   code: " & FORMAT$(pError.errorCode) & $CRLF & _
                        "   reason: " & pError.reason & $CRLF & _
                        "   errorXPath: " & pError.errorXPath & $CRLF & _
                        "Parameters count: " & FORMAT$(pError.errorParametersCount) & $CRLF
      FOR i = 0 TO pError.errorParametersCount - 1
         bstrMsg = bstrMsg & "   errorParameters(" & FORMAT$(i) & "):" & pError.errorParameters(i) & $CRLF
      NEXT
      AfxShowMsg bstrMsg
   ELSE
      AfxShowMsg "DOM is valid" & $CRLF & pXmlDoc.xml
   END IF
   pError  = NOTHING

   bstrMsg = bstrMsg & $CRLF
   bstrMsg = bstrMsg & "Validating nodes..." & $CRLF
   pNodes = pXmlDoc.selectNodes("//book")
   FOR i = 0 TO pNodes.length - 1
      pNode = pNodes.Item(i)
      pError = pXmlDoc.validateNode(pNode)
      IF pError.errorCode <> 0 THEN
         bstrMsg = bstrMsg & $CRLF
         bstrMsg = bstrMsg & "Node is invalid: " & $CRLF
         bstrMsg = bstrMsg & "   reason: " & pError.reason & $CRLF
         bstrMsg = bstrMsg & "   errorXPath: " & pError.errorXPath & $CRLF
         bstrMsg = bstrMsg & "Parameters count: " & FORMAT$(pError.errorParametersCount) & $CRLF
         FOR x = 0 TO pError.errorParametersCount - 1
            bstrMsg = bstrMsg & "   errorParameter(" & FORMAT$(x) & "):" & pError.errorParameters(x) & $CRLF
         NEXT
         AfxShowMsg bstrMsg
      ELSE
         bstrMsg = bstrMsg & "Node is valid:"
         bstrMsg = bstrMsg & pNode.xml
      END IF
   NEXT
   pError = NOTHING

   pSCache = NOTHING
   pXmlDoc = NOTHING

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


José Roca


' ========================================================================================
' Demonstrates the use of the errorParametersCount property.
' This sample code uses features that were first implemented in MSXML 5.0 for Microsoft
' Office Applications.
' ========================================================================================

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

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

   LOCAL pXmlDoc     AS IXMLDOMDocument3
   LOCAL pSCache     AS IXMLDOMSchemaCollection
   LOCAL pError      AS IXMLDOMParseError2
   LOCAL pNodes      AS IXMLDOMNodeList
   LOCAL pNode       AS IXMLDOMNode
   LOCAL bstrMsg     AS WSTRING
   LOCAL i           AS LONG
   LOCAL x           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
      pError = pXmlDoc.parseError
      AfxShowMsg "Failed to load DOM from books.xml" & $CRLF & pError.reason
      pError = NOTHING
      EXIT FUNCTION
   END IF

   ' Validate the entire DOM object
   pError = pXmlDoc.validate
   bstrMsg = "Validating DOM..." & $CRLF
   IF pError.errorCode <> 0 THEN
      bstrMsg = bstrMsg & "invalid DOM:" & $CRLF & _
                        "   code: " & FORMAT$(pError.errorCode) & $CRLF & _
                        "   reason: " & pError.reason & $CRLF & _
                        "   errorXPath: " & pError.errorXPath & $CRLF & _
                        "Parameters count: " & FORMAT$(pError.errorParametersCount) & $CRLF
      FOR i = 0 TO pError.errorParametersCount - 1
         bstrMsg = bstrMsg & "   errorParameters(" & FORMAT$(i) & "):" & pError.errorParameters(i) & $CRLF
      NEXT
      AfxShowMsg bstrMsg
   ELSE
      AfxShowMsg "DOM is valid" & $CRLF & pXmlDoc.xml
   END IF
   pError  = NOTHING

   bstrMsg = bstrMsg & $CRLF
   bstrMsg = bstrMsg & "Validating nodes..." & $CRLF
   pNodes = pXmlDoc.selectNodes("//book")
   FOR i = 0 TO pNodes.length - 1
      pNode = pNodes.Item(i)
      pError = pXmlDoc.validateNode(pNode)
      IF pError.errorCode <> 0 THEN
         bstrMsg = bstrMsg & $CRLF
         bstrMsg = bstrMsg & "Node is invalid: " & $CRLF
         bstrMsg = bstrMsg & "   reason: " & pError.reason & $CRLF
         bstrMsg = bstrMsg & "   errorXPath: " & pError.errorXPath & $CRLF
         bstrMsg = bstrMsg & "Parameters count: " & FORMAT$(pError.errorParametersCount) & $CRLF
         FOR x = 0 TO pError.errorParametersCount - 1
            bstrMsg = bstrMsg & "   errorParameter(" & FORMAT$(x) & "):" & pError.errorParameters(x) & $CRLF
         NEXT
         AfxShowMsg bstrMsg
      ELSE
         bstrMsg = bstrMsg & "Node is valid:"
         bstrMsg = bstrMsg & pNode.xml
      END IF
   NEXT
   pError = NOTHING

   pSCache = NOTHING
   pXmlDoc = NOTHING

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


José Roca


' ========================================================================================
' Demonstrates the use of the errorXPath property.
' ========================================================================================

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

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

   LOCAL pXmlDoc     AS IXMLDOMDocument3
   LOCAL pSCache     AS IXMLDOMSchemaCollection
   LOCAL pError      AS IXMLDOMParseError2
   LOCAL bstrMsg     AS WSTRING

   ' 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

   ' Load books.xml
   pXmlDoc.load("books.xml")
   IF pXmlDoc.parseError.errorCode THEN
      AfxShowMsg "Failed to load DOM from books.xml" & $CRLF & pXmlDoc.parseError.reason
      EXIT FUNCTION
   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
      AfxShowMsg bstrMsg
   ELSE
      AfxShowMsg "DOM is valid."
   END IF

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


José Roca


' ========================================================================================
' Demonstrates the use of the expr property.
' ========================================================================================

#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><Name>Microsoft</Name></Customer>")
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pXmlDoc.setProperty "SelectionLanguage", "XPath"
         pSelection = pXmlDoc.selectNodes("Customer/Name")
         AfxShowMsg pSelection.expr & " --- " & pSelection.item(0).xml
         pSelection.expr = "/Customer"
         AfxShowMsg pSelection.expr & " --- " & pselection.item(0).xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


' ========================================================================================
' Demonstrates the use of the filepos property.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pElement AS IXMLDOMElement

   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 - File position =" & STR$(pXmlDoc.parseError.filepos)
      ELSE
         pElement = pXmlDoc.documentElement
         AfxShowMsg pElement.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


' ========================================================================================
' Demonstrates the use of fistChild property.
' The following example sets pCurrNode to the first child node of the top-level node.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pCurrNode 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
         pCurrNode = pXmlDoc.documentELement.firstChild
         AfxShowMsg pCurrNode.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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



' ========================================================================================
' Demonstrates the use of fistChild property.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pRootNode AS IXMLDOMElement
   LOCAL pObjText AS IXMLDOMText
   LOCAL pFirstChild 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
         pObjText = pXmlDOc.createTextNode("Hello World!")
         pFirstChild = pRootNode.firstChild
         pRootNode.insertBefore pObjText, pFirstChild
         AfxShowMsg pRootNode.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


' ========================================================================================
' Demonstrates the use of the get method.
' Notes: This method is deprecated in MSXML 6.0, where it throws a Not Implemented
' exception. Instead of using this method in MSXML 6.0, use the getSchema Method.
' 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 pSchemaCache AS IXMLDOMSchemaCollection
   LOCAL pDOMNode AS IXMLDOMNode
   LOCAL nsTarget AS STRING

   ' Must use version 3.0 or 4.0
   pSchemaCache = NEWCOM "Msxml2.XMLSchemaCache.4.0"
   IF ISNOTHING(pSchemaCache) THEN EXIT FUNCTION

   TRY
      nsTarget = ""
      pSchemaCache.add nsTarget, "rootChild.xdr"
      pDOMNode = pSchemaCache.get(nsTarget)
      AfxShowMsg pDOMNode.xml
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


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

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pNodeBook AS IXMLDOMNode
   LOCAL pDOMElement AS IXMLDOMElement
   LOCAL vValue AS VARIANT

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

   TRY
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pNodeBook = pXmlDoc.selectSingleNode("//book")
         pDOMElement = pNodeBook
         vValue = pDOMElement.getAttribute("id")
         AfxShowMsg VARIANT$$(vValue)
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


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

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pNodeBook AS IXMLDOMNode
   LOCAL pDOMElement AS IXMLDOMElement
   LOCAL pNodeId AS IXMLDOMAttribute

   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
         pNodeBook = pXmlDoc.selectSingleNode("//book")
         pDOMElement = pNodeBook
         pNodeId = pDOMElement.getAttributeNode("id")
         AfxShowMsg pNodeId.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


' ========================================================================================
' Demonstrates the use of the getDeclaration method.
' Only works with version 4.0.
' The syntax IXMLDOMSchemaCollection2_getDeclaration(pSchemaCollection, pRootNode)
' is no longer supported and will return %E_NOTIMPL.
' When used with the sample XML (doc.xml) and schema file (doc.xsd) files, this example
' returns the namespace URI for the schema declared in doc.xml:
' http://xsdtesting
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pRootNode AS IXMLDOMElement
   LOCAL pSchemaCollection AS IXMLDOMSchemaCollection2
   LOCAL pSchemaItem AS ISchemaItem

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

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.validateOnParse = %VARIANT_FALSE
      pXmLDoc.setProperty "SelectionLanguage", "XPath"
      pXmlDoc.load "doc.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         ' Retrieve the namespace URI for schema used in the XML document.
         pRootNode = pXmlDoc.documentElement
         pSchemaCollection = pXmlDoc.namespaces
         pSchemaItem = pSchemaCollection.getDeclaration(pRootNode)
         AfxShowMsg pSchemaItem.namespaceURI
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


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

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pNodeList AS IXMLDOMNodeList
   LOCAL bstrOutput AS STRING
   LOCAL i AS LONG

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISFALSE ISOBJECT(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
         pNodeList = pXmlDoc.getElementsByTagName("author")
         FOR i = 0 TO pNodeList.length - 1
            bstrOutput += pNodeList.item(i).xml & $CRLF
         NEXT
         AfxShowMsg bstrOutput
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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