• 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 getElementsByTagName (IXMLDOMElement) method.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pNodeBook AS IXMLDOMNode
   LOCAL pNodeList AS IXMLDOMNodeList
   LOCAL pElement AS IXMLDOMElement
   LOCAL pDOMNode AS IXMLDOMNode

   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
         pNodeBook = pXmlDoc.selectSingleNode("//book")
         pElEment = pNodeBook
         pNodeList = pElement.getElementsByTagName("author")
         pDOMNode = pNodeList.item(0)
         AfxShowMsg pDOMNode.text
         ' Note: The above 3 lines can be replaced by:
         ' AfxShowMsg pElement.getElementsByTagName("author").item(0).text
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


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

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pParseError AS IXMLDOMParseError
   LOCAL pNodeBook AS IXMLDOMNode
   LOCAL pNodeId AS IXMLDOMNode
   LOCAL vIdValue AS VARIANT

   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")
         pNodeId = pNodeBook.attributes.getNamedItem("id")
         vIdValue = pNodeId.nodeValue
         AfxShowMsg VARIANT$$(vIdValue)
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


' ========================================================================================
' Demonstrates the use of the getOption method.
' This example returns the current value of the %SXH_OPTION_IGNORE_SERVER_SSL_CERT_ERROR_FLAGS
' (option 2), which by default is 13056. This value maps to the
' %SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS flag, indicating that the current XMLHTTP
' server instance will return all certificate errors.
' ========================================================================================

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

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

   LOCAL pXmlServerHttp AS IServerXMLHTTPRequest
   LOCAL vValue AS VARIANT

   pXmlServerHttp = NEWCOM "Msxml2.ServerXMLHTTP.6.0"
   IF ISNOTHING(pXmlServerHttp) THEN EXIT FUNCTION

   vValue = pXmlServerHttp.getOption(2)
   AfxShowMsg STR$(VARIANT#(vValue))

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


José Roca


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

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pSelection AS IXMLDOMSelection
   LOCAL vValue AS VARIANT

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

   TRY
      pXmlDoc.setProperty "SelectionLanguage", "XPath"
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pSelection = pXmlDoc.selectNodes("//book")
         vValue = pSelection.getProperty("SelectionLanguage")
         AfxShowMsg VARIANT$$(vValue)
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


' ========================================================================================
' Demonstrates the use of the getProperty (IXMLDOMDocument2) method.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL vValue AS VARIANT

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
   pXmlDoc.setProperty "SelectionLanguage", "XPath"
   vValue = pXmlDoc.getProperty("SelectionLanguage")
   AfxShowMsg VARIANT$$(vValue)

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


José Roca


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

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pNodeBook AS IXMLDOMNode
   LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
   LOCAL pNodeId AS IXMLDOMNode
   LOCAL vValue AS VARIANT

   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
         pNodeBook = pXmlDoc.selectSingleNode("//book")
         pNamedNodeMap = pNodeBook.attributes
         pNodeId = pNamedNodeMap.getQualifiedItem("id", "")
         vValue = pNodeId.nodeValue
         AfxShowMsg VARIANT$$(vValue)
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


' ========================================================================================
' Demonstrates the use of the getSchema method.
' The following example shows the getSchema method being used to return a schema object.
' When used with the sample schema file (doc.xsd) file above, the examples in this topic
' return the namespace URI for the schema:
' http://xsdtesting
' ========================================================================================

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

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

   LOCAL pSchemaCache AS IXMLDOMSchemaCollection2
   LOCAL pSchema AS ISchema
   LOCAL nsTarget AS STRING

   pSchemaCache = NEWCOM "Msxml2.XMLSchemaCache.6.0"
   IF ISFALSE ISOBJECT(pSchemaCache) THEN EXIT FUNCTION

   nsTarget = "http://xsdtesting"
   pSchemaCache.add nsTarget, "doc.xsd"
   pSchema = pSchemaCache.getSchema(nsTarget)
   AfxShowMsg pSchema.targetNamespace

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


José Roca


' ========================================================================================
' Demonstrates the use of hasChildNodes method
' The following example checks a node to see if it has any child nodes. If it does,
' it displays the number of child nodes it contains.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   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
         IF pCurrNode.hasChildNodes THEN
            AfxShowMsg STR$(pCurrNode.childNodes.length)
         ELSE
            AfxShowMsg "No child nodes."
         END IF
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


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

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL iFeature AS INTEGER

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

   iFeature = pXmlDoc.implementation.hasFeature("DOM", "1.0")
   AfxShowMsg "Has feature: " & FORMAT$(iFeature)

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


José Roca


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

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pImplementation AS IXMLDOMImplementation

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

   pImplementation = pXmlDoc.implementation
   AfxShowMsg "Implementation reference pointer = " & STR$(OBJPTR(pImplementation))

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


José Roca


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

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

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

   LOCAL FreeThreadedDOMDocument AS IXMLDOMDocument3
   LOCAL pDOMDocument AS IXMLDOMDocument3
   LOCAL pIXMLDOMNode AS IXMLDOMNode
   LOCAL pCloneNode AS IXMLDOMNode
   LOCAL bstrMsg AS WSTRING
   LOCAL pElement AS IXMLDOMElement
   LOCAL pTextNode AS IXMLDOMNode

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

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

   pDOMDocument.async = %VARIANT_FALSE
   IF ISFALSE pDOMDocument.Load("doc1.xml") THEN
      AfxShowMsg "Can't load doc1.xml"
      EXIT FUNCTION
   END IF

   FreeThreadedDOMDocument.async = %VARIANT_FALSE
   IF ISFALSE FreeThreadedDOMDocument.Load("doc2.xml") THEN
      AfxShowMsg "Can't load doc2.xml"
      EXIT FUNCTION
   END IF

   ' Copy a node from FreeThreadedDOMDocument to pDOMDocument:
   '   Fetch the "/doc" (node) from FreeThreadedDOMDocument (doc2.xml).
   '   Clone node for import to pDOMDocument.
   '   Append clone to pDOMDocument (doc1.xml).

   pIXMLDOMNode = FreeThreadedDOMDocument.selectSingleNode("/doc")
   pCloneNode = pDOMDocument.importNode(pIXMLDOMNode, %VARIANT_TRUE)
   pElement = pDOMDocument.documentElement
   pElement.appendChild pCloneNode
   pTextNode = pDOMDocument.createTextNode($CRLF)
   pElement.appendChild pTextNode
   pElement = NOTHING
   pIXMLDOMNode = NOTHING
   pCloneNode = NOTHING

   bstrMsg = bstrMsg + "doc1.xml after importing /doc from doc2.xml:"
   bstrMsg = bstrMsg + $CRLF & pDOMDocument.xml & $CRLF

   ' Clone a node using importNode() and append it to self:
   '   Fetch the "doc/b" (node) from pDOMDocument (doc1.xml).
   '   Clone node using importNode() on pDOMDocument.
   '   Append clone to pDOMDocument (doc1.xml).

   pIXMLDOMNode = FreeThreadedDOMDocument.selectSingleNode("/doc/b")
   pCloneNode = pDOMDocument.importNode(pIXMLDOMNode, %VARIANT_TRUE)
   pElement = pDOMDocument.documentElement
   pElement.appendChild pCloneNode
   pTextNode = pDOMDocument.createTextNode($TAB)
   pElement.appendChild pTextNode
   pElement = NOTHING
   pIXMLDOMNode = NOTHING
   pCloneNode = NOTHING

   bstrMsg = bstrMsg & "doc1.xml after import /doc/b from self:"
   bstrMsg = bstrMsg & $CRLF & pDOMDocument.xml & $CRLF

   ' Clone a node and append it to the dom using cloneNode():
   '   Fetch "doc/a" (node) from pDOMDocument (doc1.xml).
   '   Clone node using cloneNode on pDOMDocument.
   '   Append clone to pDOMDocument (doc1.xml).

   pIXMLDOMNode = FreeThreadedDOMDocument.selectSingleNode("/doc/a")
   pCloneNode = pDOMDocument.importNode(pIXMLDOMNode, %VARIANT_TRUE)
   pElement = pDOMDocument.documentElement
   pElement.appendChild pCloneNode
   pTextNode = pDOMDocument.createTextNode($TAB)
   pElement.appendChild pTextNode
   pElement = NOTHING
   pIXMLDOMNode = NOTHING
   pCloneNode = NOTHING

   bstrMsg = bstrMsg & "doc1.xml after cloning /doc/a from self:"
   bstrMsg = bstrMsg & $CRLF + pDOMDocument.xml & $CRLF

   pDOMDocument.save "out.xml"
   bstrMsg = bstrMsg + "a new document was saved to out.xml in the current working directory."
   AfxShowMsg bstrMsg

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


José Roca


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

#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 vRes 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
      pXmlDoc.async = %VARIANT_FALSE
      IF ISTRUE pXslDoc.load("Sample2.xsl") THEN
         pXslt.putref_stylesheet = pXslDoc
         pXmlDoc.async = %FALSE
         IF ISTRUE pXmLDoc.load("books.xml") THEN
            pXslProc = pXslt.createProcessor
            pXslProc.input = pXmlDoc
            pXslProc.transform
            vRes = pXslProc.output
            AfxShowMsg VARIANT$$(vRes)
         END IF
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

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


José Roca


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

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pPri AS IXMLDOMProcessingInstruction
   LOCAL pNodeList AS IXMLDOMNodeList
   LOCAL pItem AS IXMLDOMNode

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

   IF ISTRUE pXmlDoc.load("books.xml") THEN
pPri = pXmlDoc.createProcessingInstruction("xml", "version=""1.0""")
      pNodeList = pXmlDoc.childNodes
      pItem = pNodeList.item(0)
      pXmlDoc.insertBefore pPri, pItem
      AfxShowMsg pXmlDoc.xml
   END IF

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



' ========================================================================================
' Demonstrates the use of the insertBefore method.
' The following example creates a new IXMLDOMNode object and inserts it as the second
' child of the top-level node.
' ========================================================================================

#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 pItem AS IXMLDOMNode
   LOCAL pCurrNode AS IXMLDOMNode
   LOCAL pNewNode AS IXMLDOMNode

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

   pXmlDoc.loadXML "<root><child1/></root>"
   pRootNode = pXmlDoc.documentElement
   AfxShowMsg pRootNode.xml
   pNewNode = pXmlDoc.createNode(%NODE_ELEMENT, "CHILD2", "")
   pNodeList = pRootNode.childNodes
   pItem = pNodeList.item(0)
   pCurrNode = pRootNode.insertBefore(pNewNode, pItem)
   AfxShowMsg pRootNode.xml

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


José Roca


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

#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

   pXmlDoc.async = %VARIANT_FALSE
   IF ISTRUE pXmlDoc.load("books.xml") THEN
      pComment = pXmlDoc.createComment("Hello...")
      pComment.insertData 6, "Beautiful"
      AfxShowMsg pComment.xml
   END IF

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


José Roca


' ========================================================================================
' Demonstrates the use of the item method.
' The following example creates an IXMLDOMNodeList object with the document's
' getElementsByTagName method. It then iterates through the collection, displaying the
' text value of each item in the list.
' ========================================================================================

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

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

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pNodeList AS IXMLDOMNodeList
   LOCAL bstrOut AS WSTRING
   LOCAL i 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
      pNodeList = pXmlDoc.getElementsByTagName("author")
      FOR i = 0 TO pNodeList.length - 1
         bstrOut += pNodeList.item(i).text & $CRLF
      NEXT
      AfxShowMsg bstrOut
   END IF

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