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 item method (IXMLDOMNamedNodeMap).
' The following example creates an IXMLDOMNamedNodeMap object to retrieve the attributes
' for an element node selected using the SelectSingleNode method. It then iterates through
' the attributes, before displaying the name and value of each attribute in the collection.
' =========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pBookNode AS IXMLDOMNode
   LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
   LOCAL pItem AS IXMLDOMNode
   LOCAL iLen AS LONG
   LOCAL i AS LONG
   LOCAL vValue AS VARIANT

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

   pXmlDoc.async = %VARIANT_FALSE
   IF ISTRUE pXmlDoc.load("books.xml") THEN
      pBookNode = pXmlDoc.selectSingleNode("//book")
      pNamedNodeMap = pBookNode.attributes
      iLen = pNamedNodeMap.length
      FOR i = 0 TO iLen - 1
         pItem = pNamedNodeMap.item(i)
         AfxShowMsg "Attribute name: " & pItem.nodeName
         vValue = pItem.nodeValue
         AfxShowMsg "Attribute value: " & VARIANT$$(vValue)
         pItem = NOTHING
      NEXT
   END IF

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

  •  

José Roca


' ========================================================================================
' Demonstrates the use of various methods of the IXMLDOMSchemaCollection interface.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pSchemaCache AS IXMLDOMSchemaCollection2
   LOCAL pSchemaNode AS IXMLDOMNode
   LOCAL nsTarget AS WSTRING

   ' Must use version 4.0
   pXmlDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.4.0"
   IF ISFALSE ISOBJECT(pXmlDoc) THEN EXIT FUNCTION
   pSchemaCache = NEWCOM "Msxml2.XMLSchemaCache.4.0"
   IF ISFALSE ISOBJECT(pSchemaCache) THEN EXIT FUNCTION

   pXmlDoc.async = %VARIANT_FALSE
   pXmlDoc.validateOnParse = %VARIANT_TRUE

   nsTarget = "myURI"
   pSchemaCache.add nsTarget, "books.xsd"
   pSchemaNode = pSchemaCache.get(nsTarget)
   ' Validate the collection
   pSchemaCache.validate
   IF OBJRESULT THEN AfxShowMsg "Validate failed"
   ' Get the namespaceURI
   AfxShowMsg pSchemaCache.namespaceURI(0)
   ' Get the length of the collection
   AfxShowMsg STR$(pSchemaCache.length)
   ' Remove the namesapce
   pSchemaCache.remove nsTarget
   ' Get again the length of the collection
   AfxShowMsg STR$(pSchemaCache.length)

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

  •  

José Roca


' =========================================================================================
' Demonstrates the use of the lastChild 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 pNewNode AS IXMLDOMNode
   LOCAL pCurrNode AS IXMLDOMNode
   LOCAL pLastChild AS IXMLDOMNode

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

   pXmlDoc.async = %VARIANT_FALSE
   IF ISTRUE pXmlDoc.load("books.xml") THEN
      pRootNode = pXmlDoc.documentElement
      AfxShowMsg pRootNode.xml
      pNewNode = pXmlDoc.createNode(%NODE_ELEMENT, "VIDEOS", "")
      pLastChild = pRootNode.lastChild
      pCurrNode = pRootNode.insertBefore(pNewNode, pLastChild)
      AfxShowMsg pRootNode.xml
   END IF

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

  •  

José Roca


' =========================================================================================
' Demonstrates the use of the length property (IXMLDOMCharacterData).
' The following example creates an IXMLDOMComment object and then assigns the length
' property to a variable.
' =========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pComment AS IXMLDOMComment
   LOCAL lValue AS LONG

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

   pXmlDoc.async = %VARIANT_FALSE
   IF ISTRUE pXmlDoc.load("books.xml") THEN
      pComment = pXmLDoc.createComment("Hello World!")
      lValue = pComment.length
      AfxShowMsg STR$(lValue)
   END IF

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

  •  

José Roca


' =========================================================================================
' Demonstrates the use of the length property (IXMLDOMNamedNodeMap).
' Returns the number of items in the collection.
' =========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pNodeBook AS IXMLDOMNode
   LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap

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

   pXmlDoc.setProperty "SelectionLanguage", "XPath"
   pXmlDoc.async = %VARIANT_FALSE
   IF ISTRUE pXmlDoc.load("books.xml") THEN
      pNodeBook = pXmlDoc.selectSingleNode("//book")
      pNamedNodeMap = pNodeBook.attributes
      AfxShowMsg STR$(pNamedNodeMap.length)
   END IF

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

  •  

José Roca


' ========================================================================================
' Demonstrates the use of the length property (IXMLDOMNodeList).
' The following example creates an IXMLDOMNodeList object and then uses its length
' property to support iteration.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pNodeList AS IXMLDOMNodeList
   LOCAL pItem AS IXMLDOMNode
   LOCAL bstrOut AS STRING
   LOCAL i AS LONG

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

   pXmLDoc.async = %FALSE
   IF ISTRUE pXmlDoc.load("books.xml") THEN
      pNodeList = pXmlDoc.getElementsByTagName("author")
      FOR i = 0 TO pNodeList.length - 1
         bstrOut += pNodeList.item(i).text & $CRLF
      NEXT
      AfxShowMsg bstrOut
   END IF

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

  •  

José Roca


' ========================================================================================
' Demonstrates the use of length property (XMLSchemaCache/IXMLSchemaCollection).
' ========================================================================================

#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 IXMLDOMSchemaCollection
   LOCAL strNamespaceURI AS STRING

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

   pXmLDoc.async = %VARIANT_FALSE
   IF ISTRUE pXmlDoc.load("books2.xml") THEN
      ' Get the namespace of the root element.
      pRootNode = pXmlDoc.documentElement
      strNamespaceURI = pRootNode.namespaceURI
      pSchemaCollection = pXmlDoc.namespaces
      AfxShowMsg STR$(pSchemaCollection.length)
   END IF

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

  •  

José Roca


' ========================================================================================
' Demonstrates the use of the length (IXMLDOMParseErrorCollection) property.
' The following code performs an XSD validation on an XML document that has two invalid
' <book> nodes. The code then outputs the number of errors in the resultant error
' collection.
' ========================================================================================

#DIM ALL
#DEBUG ERROR ON
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

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

   LOCAL pXMLDoc     AS IXMLDOMDocument3
   LOCAL pSCache     AS IXMLDOMSchemaCollection
   LOCAL pError      AS IXMLDOMParseError2
   LOCAL pErrors     AS IXMLDOMParseErrorCollection
   LOCAL bstrMsg     AS WSTRING
   LOCAL errsCount   AS LONG

   ' Create an instance of XML DOM
   pXMLDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXMLDoc) THEN EXIT FUNCTION

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

   ' Add "urn:books" from "books.xsd" to schema caché
   pSCache.add "urn:books", "books.xsd"

   ' Set the reference
   pXMLDoc.putref_schemas = pSCache

   ' Set the MultipleErrorMessages property
   pXMLDoc.async = %VARIANT_FALSE
   pXMLDoc.validateOnParse = %VARIANT_FALSE
   pXMLdoc.setProperty "MultipleErrorMessages", %VARIANT_TRUE

   ' 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
   IF pError.errorCode <> 0 THEN
      pErrors = pError.allErrors
      IF ISOBJECT(pErrors) THEN
         errsCount = pErrors.length
         AfxShowMsg "There are " & STR$(errsCount) & " errors in the collection"
         pErrors = NOTHING
      END IF
   ELSE
      AfxShowMsg "DOM is valid " & $CRLF & pXmlDoc.xml
   END IF

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

  •  

José Roca


' ========================================================================================
' Demonstrates the use of the line 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 " & pXmlDoc.parseError.reason
      ELSE
         pElement = pXmlDoc.documentElement
         AfxShowMsg pElement.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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

  •  

José Roca


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

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pError AS IXMLDOMParseError
   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"
      pError = pXMLDoc.parseError
      IF pError.errorCode THEN
         AfxShowMsg "A parse error occurred on line " & STR$(pError.line) & _
                    " at position " & STR$(pError.linePos)
      ELSE
         pElement = pXmlDoc.documentElement
         AfxShowMsg pElement.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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

  •  

José Roca


' ========================================================================================
' Demonstrates the use of the load method.
' The following example creates a DOMDocument object and uses the load method to load a
' local XML file.
' ========================================================================================

#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 "books.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 loadXML method.
' The following example creates a DOMDocument object, and then uses its loadXML method to
' load the specified XML before displaying it.
' ========================================================================================

#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.loadXml "<customer>" & _
                      "<first_name>Joe</first_name>" & _
                      "<last_name>Smith</last_name></customer>"
      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 matches 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 pSelection AS IXMLDOMSelection
   LOCAL pNode AS IXMLDOMNode

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

   TRY
      pXmlDoc.setProperty "SelectionLanguage", "XPath"
      pXmlDoc.async = %FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pNodeBook = pXmlDoc.selectSingleNode("//book")
         pSelection = pXmlDoc.selectNodes("//*")
         pNode = pSelection.matches(pNodeBook)
         AfxShowMsg pNode.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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

  •  

José Roca


' ========================================================================================
' Demonstrates the use of name property (IXMLDOMAttribute).
' The following example creates an IXMLDOMAttribute object from an attribute of the first
' child of the root, and then displays the value of its name.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pRootNode AS IXMLDOMElement
   LOCAL pDOMNode AS IXMLDOMNode
   LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
   LOCAL pDOMAttr 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
         pRootNode = pXmlDoc.documentElement
         pNamedNodeMap = pRootNode.firstChild.attributes
         pDOMNode = pNamedNodeMap.item(0)
         pDOMAttr = pDOMNode
         AfxShowMsg pDOMAttr.name
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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

  •  

José Roca


' ========================================================================================
' Demonstrates the use of name property (IXMLDOMDocumentType).
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pDocType AS IXMLDOMDocumentType

   ' DTDs are disabled by default in version 6.0
   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
         AfxShowMsg pDocType.name
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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



' ========================================================================================
' Demonstrates the use of name property (IXMLDOMDocumentType).
' Sets the ProhibitDTD to false to allow the inclusion of a DTD in the XML DOM document.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pParseError AS IXMLDOMParseError
   LOCAL pDocType AS IXMLDOMDocumentType

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

   TRY
      pXmLDoc.setProperty "ProhibitDTD", %VARIANT_FALSE
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books1.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pDocType = pXmlDoc.doctype
         AfxShowMsg pDocType.name
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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

  •