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.
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' =========================================================================================
' 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
' =========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' =========================================================================================
' Demonstrates the use of the createAttribute method.
' The following example creates a new attribute called ID and adds it to the attributes of
' the DOMDocument object.
' =========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' =========================================================================================
' Main
' =========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pRootNode AS IXMLDOMElement
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pNewAtt AS IXMLDOMAttribute
LOCAL i AS LONG
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
pRootNode = pXmlDoc.documentElement
pNewAtt = pXmlDoc.createAttribute("ID")
pNamedNodeMap = pRootNode.attributes
pNamedNodeMap.setNamedItem(pNewAtt)
FOR i = 0 TO pNamedNodeMap.length - 1
AfxShowMsg pNamedNodeMap.item(i).xml
NEXT
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
' ========================================================================================
' Demonstrates the use of createCDATASection.
' The following example creates a new CDATA section node.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pNodeCDATA AS IXMLDOMCDATASection
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pNodeCDATA = pXmlDoc.createCDATASection("Hello")
AfxShowMsg pNodeCDATA.Xml
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of createComment and appendChild.
' The following example creates a new comment node and appends it as the last child node
' of the DOMDocument object.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pParseError AS IXMLDOMParseError
LOCAL pRootNode AS IXMLDOMElement
LOCAL pComment AS IXMLDOMComment
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
pRootNode = pXmlDoc.documentElement
pComment = pXmlDoc.createComment("Hello World!")
pRootNode.appendChild pComment
AfxShowMsg pRootNode.Xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of createDocumentFragment.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pDocFragment AS IXMLDOMDocumentFragment
LOCAL pRootNode AS IXMLDOMElement
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.loadXML("<root/>")
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pDocFragment = pXmlDoc.createDocumentFragment
pDocFragment.AppendChild pXmlDoc.createElement("node1")
pDocFragment.AppendChild pXmlDoc.createElement("node2")
pDocFragment.AppendChild pXmlDoc.createElement("node3")
AfxShowMsg pDocFragment.xml
' PB doesn't allow...
' pXmlDoc.documentElement.appendChild pDocFragment
' .. so we have to use an intermediate step...
pRootNode = pXmlDoc.documentElement
pRootNode.appendChild pDocFragment
AfxShowMsg pXmlDoc.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the createElement method.
' The following example creates an element called PAGES and appends it to an IXMLDOMNode
' object. It then sets the text value of the element to 400.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pRootNode AS IXMLDOMElement
LOCAL pNewElem AS IXMLDOMElement
LOCAL pDOMNode 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
pNewElem = pXmlDoc.createElement("PAGES")
pDOMNode = pRootNode.childNodes.item(1)
pDOMNode.appendChild pNewElem
pDOMNode.lastChild.text = "400"
AfxShowMsg pDOMNode.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the createElement method.
' The following example creates an element called NEW and appends it to an IXMLDOMNode
' object. It then sets the text value of the element to 123.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pRootNode AS IXMLDOMElement
LOCAL pNewElem AS IXMLDOMElement
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.loadXML("<root><child/></root>")
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pRootNode = pXmlDoc.documentElement
pNewElem = pXmlDoc.createElement("NEW")
pRootNode.childnodes.item(0).appendChild(pNewElem)
pRootNode.childnodes.item(0).lastChild.text = "123"
AfxShowMsg pRootNode.childNodes.item(0).xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of createEntityReference.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pNodeEntityReference AS IXMLDOMEntityReference
LOCAL pRootNode AS IXMLDOMElement
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmLDoc.loadXML "<root/>"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pNodeEntityReference = pXmlDoc.createEntityReference("nbsp")
pRootNode = pXmlDoc.documentElement
pRootNode.appendChild pNodeEntityReference
AfxShowMsg pXmlDoc.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of createEntityReference.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pNodeEntityReference AS IXMLDOMEntityReference
LOCAL pRootNode AS IXMLDOMElement
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmLDoc.loadXML "<root><child/></root>"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pRootNode = pXmlDoc.documentElement
pNodeEntityReference = pXmlDoc.createEntityReference("newRef")
pRootNode.childNodes.item(0).appendChild(pNodeEntityReference)
AfxShowMsg pRootNode.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' =========================================================================================
' Demonstrates the use of the createNode 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 pCurrNode AS IXMLDOMNode
LOCAL pLastChild 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
AfxShowMsg pRootNode.xml
pNewNode = pXmlDoc.createNode(%NODE_ELEMENT, "VIDEOS", "")
pLastChild = pRootNode.lastChild
pCurrNode = pRootNode.insertBefore(pNewNode, pLastChild)
AfxShowMsg pCurrNode.xml
AfxShowMsg pRootNode.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
' =========================================================================================
' Demonstrates the use of the createNode method.
' =========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' =========================================================================================
' Main
' =========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pNewNode AS IXMLDOMNode
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmLDoc.loadXml "<root><child/></root>"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pNewNode = pXmlDoc.createNode(%NODE_ATTRIBUTE, "Sci-Fi", "")
pNamedNodeMap = pXmlDoc.documentElement.childNodes.item(0).Attributes
pNamedNodeMap.setNamedItem pNewNode
AfxShowMsg pXmlDoc.documentElement.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
' ========================================================================================
' Demonstrates the use of createProcessingInstruction.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pPri AS IXMLDOMProcessingInstruction
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pPri = pXmlDoc.createProcessingInstruction("xml", "version=""1.0""")
AfxShowMsg pPri.xml
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of createProcessingInstruction.
' The following example specifies the target string "xml" and data string
' "version = '1.0'" to generate the processing instruction <?XML version="1.0"?>.
' ========================================================================================
#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
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.loadXML "<root><child/></root>"
pPri = pXmlDoc.createProcessingInstruction("xml", "version=""1.0""")
pNodeList = pXmlDoc.childNodes
pItem = pNodeList.item(0)
pXmlDoc.insertBefore pPri, pItem
AfxShowMsg pXmlDoc.xml
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' =========================================================================================
' Demonstrates the use of the createProcessor 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
pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
IF ISNOTHING(pXslt) THEN EXIT FUNCTION
pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
TRY
pXslDoc.async = %VARIANT_FALSE
pXslDoc.load "createProcessor.xsl"
IF pXslDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXslDoc.parseError.reason
ELSE
pXslt.putref_stylesheet = pXslDoc
pXmlDoc.async = %VARIANT_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
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
' =========================================================================================
' Demonstrates the use of the createTextNode 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 pTextNode AS IXMLDOMText
LOCAL pChildNode AS IXMLDOMNode
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.loadXML("<root><child/></root>")
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pRootNode = pXmlDoc.documentElement
pTextNode = pXmlDoc.createTextNode("Hello World")
pChildNode = pRootNode.childNodes.item(0)
pRootNode.insertBefore pTextNode, pChildNode
AfxShowMsg pRootNode.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
' ========================================================================================
' Demonstrates the use of the data (IXMLDOMCharacterData) property.
' The following script example walks the document tree and checks for comment node types.
' If one is found, it displays its contents with the data 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 pDOMNode AS IXMLDOMNode
LOCAL pComment AS IXMLDOMComment
LOCAL i AS LONG
pXMLDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXMLDoc) THEN EXIT FUNCTION
pXMLDoc.async = %VARIANT_FALSE
pXMLDoc.loadXML "<root><!-- Hello --></root>"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pRootNode = pXMLDoc.documentElement
FOR i = 0 TO pRootNode.childNodes.length - 1
IF pRootNode.childNodes.item(i).nodeType = %NODE_COMMENT THEN
' This doesn't work with PB because pRootNode.childNodes.item(i)
' returns a pointer to yhe IXMLDOMNode interface, not a pointer
' to the IXMLDOMComment interface.
' pComment = pRootNode.childNodes.item(i)
' MSGBOX pComment.Data
' Therefore, we need assign it to an object variable declared as
' IXMLDOMNode and then assign it to pComment, forcing an implicit
' call to QueryInterface to retrieve the correct object pointer.
pDOMNode = pRootNode.childNodes.item(i)
pComment = pDOMNode
AfxShowMsg pComment.Data
END IF
NEXT
END IF
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the data (IXMLDOMProcessingInstruction) property.
' The following example displays the node's data of the xml processing instruction.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pDOMNode AS IXMLDOMNode
LOCAL pri AS IXMLDOMProcessingInstruction
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
pDOMNode = pXMLDoc.firstChild
IF pDOMNode.nodeType = %NODE_PROCESSING_INSTRUCTION THEN
pri = pDOMNode
AfxShowMsg pri.Data
pri = NOTHING
END IF
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the dataType property.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXMLDoc AS IXMLDOMDocument
LOCAL pRootNode AS IXMLDOMElement
pXMLDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXMLDoc) THEN EXIT FUNCTION
TRY
pXMLDoc.async = %VARIANT_FALSE
pXMLDoc.loadXML("<root/>")
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pRootNode = pXMLDoc.documentElement
pRootNode.dataType = "int"
pRootNode.nodeTypedValue = 5
AfxShowMsg pXMLDoc.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' The following example shows the retrieval of the definition property from an
' IXMLDOMElement.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pIXMLDOMElement AS IXMLDOMElement
LOCAL pIXMLDOMNode AS IXMLDOMNode
' XDR schemas are not supported in MSXML 6.0.
' Therefore, we must use version 3.0 or 4.0.
pXmlDoc = NEWCOM "Msxml2.DOMDocument.4.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.load "sample.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pIXMLDOMElement = pXMLDoc.documentElement
pIXMLDOMNode = pIXMLDOMElement.definition
IF ISOBJECT(pIXMLDOMNode) THEN AfxShowMsg pIXMLDOMNode.xml
' Note: The first two lines above can be replaced by:
' pIXMLDOMNode = pXmlDoc.documentElement.definition
' And the three lines above can be replaced by:
' AfxShowMsg pXmlDoc.documentElement.definition.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the deleteData 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
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.load("books.xml")
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pComment = pXmlDoc.createComment("Hello World!")
pComment.deleteData 6, 6
AfxShowMsg pComment.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the deleteData method.
' The following example creates an IXMLDOMComment object and uses the deleteData method to
' delete three characters of data starting after the third character in data for the
' IXMLDOMComment object.
' ========================================================================================
#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><child/></root>")
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pComment = pXmlDoc.createComment("123456789")
pComment.deleteData 3, 3
AfxShowMsg pComment.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' =========================================================================================
' 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
' =========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' =========================================================================================
' 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
' =========================================================================================
' =========================================================================================
' 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
' =========================================================================================
' =========================================================================================
' 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
' =========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' 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
' ========================================================================================
' ========================================================================================
' Demonstrates the use of namespaces property.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pSchemaCollection AS IXMLDOMSchemaCollection
LOCAL strNamespaceURI AS STRING
LOCAL i AS LONG
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.load "books2.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pSchemaCollection = pXmlDoc.namespaces
FOR i = 0 TO pSchemaCollection.length - 1
AfxShowMsg pSchemaCollection.namespaceURI(i)
NEXT
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of namespaceURI property (XMLSchemaCache/IXMLSchemaCollection).
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pSchemaCollection AS IXMLDOMSchemaCollection
LOCAL strNamespaceURI AS STRING
LOCAL i AS LONG
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.load "books2.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pSchemaCollection = pXmlDoc.namespaces
FOR i = 0 TO pSchemaCollection.length - 1
AfxShowMsg pSchemaCollection.namespaceURI(i)
NEXT
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of namespaceURI property (IXMLDOMNode).
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pRootNode AS IXMLDOMElement
LOCAL strNamespaceURI AS STRING
LOCAL i AS LONG
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.load "books2.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
' Get the namespace of the root element.
pRootNode = pXmlDoc.documentElement
strNamespaceURI = pRootNode.namespaceURI
AfxShowMsg strNamespaceURI
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the NewParser property.
' The following example sets the validateOnParse property to false to allow the use of the
' new parser width DTD.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
pXmlDoc = NEWCOM "Msxml2.DOMDocument.4.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.setProperty "NewParser", %VARIANT_TRUE
pXmlDoc.validateOnParse = %VARIANT_FALSE
pXmlDoc.loadXML "<?xml version='1.0' ?><!DOCTYPE root [<!ELEMENT root (#PCDATA)>]><root a='bc'>abc</root>"
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
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the NewParser property.
' The following example sets the validateOnParse property to true to demostrate the lack
' of DTD validation of the new parser.
' ========================================================================================
#DIM ALL
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
pXmlDoc = NEWCOM "Msxml2.DOMDocument.4.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.setProperty "NewParser", %VARIANT_TRUE
pXmlDoc.validateOnParse = %VARIANT_FALSE
pXmlDoc.loadXML "<?xml version='1.0' ?><!DOCTYPE root [<!ELEMENT root (#PCDATA)>]><root a='bc'>abc</root>"
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
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the NewParser property.
' The following example sets the ProhibitDTD and the validateOnParse properties to false
' to allow to use MSXML 6.0 with the new parser
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.setProperty "NewParser", %VARIANT_TRUE
pXmlDoc.setProperty "ProhibitDTD", %VARIANT_FALSE
pXmlDoc.validateOnParse = %VARIANT_FALSE
pXmlDoc.loadXML "<?xml version='1.0' ?><!DOCTYPE root [<!ELEMENT root (#PCDATA)>]><root a='bc'>abc</root>"
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
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the next 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 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
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
pErrors = pError.allErrors
IF ISOBJECT(pErrors) THEN
DO
pEItem = pErrors.next
IF ISNOTHING(pEItem) THEN EXIT DO
bstrErrors += "errorItem[" & FORMAT$(i) & "]: " & pEItem.reason & $CRLF
pEItem = NOTHING
i = i + 1
LOOP
pErrors = NOTHING
END IF
AfxShowMsg bstrMsg & bstrErrors
ELSE
AfxShowMsg "DOM is valid"
END IF
END FUNCTION
' ========================================================================================
' =========================================================================================
' Demonstrates the use of the nextNode method.
' The following example creates an IXMLDOMNamedNodeMap object and uses its nextNode method
' to iterate the collection.
' =========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' =========================================================================================
' Main
' =========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pParseError AS IXMLDOMParseError
LOCAL pRootNode AS IXMLDOMElement
LOCAL pCurrNode AS IXMLDOMNode
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pDOMNode 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
pCurrNode = pRootNode.childNodes.item(1)
pNamedNodeMap = pCurrNode.attributes
FOR i = 0 TO pNamedNodeMap.length - 1
pDOMNode = pNamedNodeMap.nextNode
AfxShowMsg pDOMNode.text
pDOMNode = NOTHING
NEXT
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
' =========================================================================================
' Demonstrates the use of the nextNode method.
' The following example creates an IXMLDOMNodeList object and uses its nextNode method to
' iterate the collection.
' =========================================================================================
#DIM ALL
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' =========================================================================================
' Main
' =========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pDOMNodeList AS IXMLDOMNodeList
LOCAL pDOMNode AS IXMLDOMNode
LOCAL bstrOut AS WSTRING
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
pDOMNodeList = pXmlDoc.getElementsByTagName("author")
FOR i = 0 TO pDOMNodeList.length - 1
pDOMNode = pDOMNodeList.nextNode
bstrOut += pDOMNode.text & $CRLF
pDOMNode = NOTHING
NEXT
AfxShowMsg bstrOut
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
' =========================================================================================
' Demonstrates the use of the nextSibling property.
' =========================================================================================
#DIM ALL
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' =========================================================================================
' Main
' =========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pRootNode AS IXMLDOMElement
LOCAL pCurrNode AS IXMLDOMNode
LOCAL pNextNode 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(0)
pNextNode = pCurrNode.nextSibling
AfxShowMsg pNextNode.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
' =========================================================================================
' Demonstrates the use of the nodeFromID method.
' Note: The following calls are needed to work with version 6.0.
' They aren't needed in versions 2.0 or 4.0.
' pXmlDoc.setProperty "ProhibitDTD", %VARIANT_FALSE
' pXmlDoc.resolveExternals %VARIANT_TRUE
' =========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' =========================================================================================
' Main
' =========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
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.resolveExternals = %VARIANT_TRUE
pXmlDoc.load "people2.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pDOMNode = pXmlDoc.nodeFromID("p3")
AfxShowMsg pDOMNode.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
' ========================================================================================
' Demonstrates the use of the nodename 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 pDOMNode 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
pDOMNode = pRootNode.firstChild.attributes.item(0)
AfxShowMsg pDOMNode.nodename
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the nodename property.
' The following example creates an IXMLDOMNode object and displays 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 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
pRootNode = pXmlDoc.documentElement
pCurrNode = pRootNode.childNodes.item(0)
AfxShowMsg pCurrNode.nodename
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the nodeType 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 pCurrNode AS IXMLDOMNode
LOCAL vVal AS VARIANT
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.loadXML "<root><!-- Hello --></root>"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pRootNode = pXmlDoc.documentElement
pCurrNode = pRootNode.childNodes.item(0)
IF pCurrNode.nodeType = %NODE_COMMENT THEN
vVal = pCurrNode.nodeValue
AfxShowMsg VARIANT$$(vVal)
END IF
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the nodeType property.
' The following example creates an IXMLDOMNode object and displays its type enumeration,
' in this case, 1 for NODE_ELEMENT.
' ========================================================================================
#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
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(0)
AfxShowMsg STR$(pCurrNode.nodeType)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' =======================================================================================
' Demonstrates the use of the nodeTypedValue property.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pXmlDocTest AS IXMLDOMDocument
LOCAL pRootNode AS IXMLDOMElement
LOCAL pChildNode AS IXMLDOMNode
pXmlDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pXmlDocTest = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXmlDocTest) THEN EXIT FUNCTION
pRootNode = pXmlDoc.createElement("Test")
pXmlDoc.putref_documentElement = pRootNode
pChildNode = pXmLDoc.createNode(%NODE_TEXT, "", "")
pRootNode.appendChild pChildNode
pRootNode.dataType = "bin.hex"
pChildNode.nodeTypedValue = "ffab123d"
pXmlDocTest.async = %FALSE
IF ISTRUE pXmlDocTest.load(pXmlDoc) THEN
AfxShowMsg pXmLDocTest.xml
END IF
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the nodeTypeString property.
' The following example creates an IXMLDOMNode object and displays its node type in string
' form, in this case, "element".
' ========================================================================================
#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
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(0)
AfxShowMsg pCurrNode.nodeTypeString
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the nodeValue property.
' The following example creates an IXMLDOMNode object and tests if it is a comment node.
' If it is, it displays its value.
' ========================================================================================
#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 strNodeType AS STRING
LOCAL vValue AS VARIANT
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.loadXML "<root><!-- Hello --></root>"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pRootNode = pXmlDoc.documentElement
pDOMNode = pRootNode.childNodes.item(0)
strNodeType = pDOMNode.nodeTypeString
IF strNodeType = "comment" THEN
vValue = pDOMNode.nodeValue
AfxShowMsg VARIANT$$(vValue)
END IF
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the normalize 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 pNodeList AS IXMLDOMNodeList
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.loadXML "<root/>"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pRootNode = pXmlDoc.documentElement
pRootNode.appendChild pXmlDoc.createTextNode("Hello ")
pRootNode.appendChild pXmlDoc.createTextNode("World")
pRootNode.appendChild pXmlDoc.createTextNode("!")
pNodeList = pRootNode.childNodes
AfxShowMsg "length = " & STR$(pNodeList.length)
pRootNode.normalize
AfxShowMsg "length = " & STR$(pNodeList.length)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the notationName 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 pEntity AS IXMLDOMEntity
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pDocType AS IXMLDOMDocumentType
LOCAL pDomNode AS IXMLDOMNode
' 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 "doment1.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pDocType = pXmlDoc.doctype
pNamedNodeMap = pDocType.entities
pDOMNode = pNamedNodeMap.nextNode
pEntity = pDOMNode
AfxShowMsg pEntity.notationName
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the notationName 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 pEntity AS IXMLDOMEntity
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pDocType AS IXMLDOMDocumentType
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
pDocType = pXmlDoc.doctype
pNamedNodeMap = pDocType.entities
pDOMNode = pNamedNodeMap.nextNode
pEntity = pDOMNode
AfxShowMsg pEntity.notationName
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the notations property.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pDocType AS IXMLDOMDocumentType
LOCAL pDomNode AS IXMLDOMNode
LOCAL pNotation AS IXMLDOMNotation
LOCAL vPublicId AS VARIANT
' 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 "doment1.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pDocType = pXmlDoc.doctype
pNamedNodeMap = pDocType.notations
pDOMNode = pNamedNodeMap.nextNode
pNotation = pDOMNode
vPublicId = pNotation.systemId
AfxShowMsg VARIANT$$(vPublicId)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the notations property.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pDocType AS IXMLDOMDocumentType
LOCAL pDomNode AS IXMLDOMNode
LOCAL pNotation AS IXMLDOMNotation
LOCAL vPublicId AS VARIANT
' DTDs are disabled by default in version 6.0
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
pDocType = pXmlDoc.doctype
pNamedNodeMap = pDocType.notations
pDOMNode = pNamedNodeMap.nextNode
pNotation = pDOMNode
vPublicId = pNotation.systemId
AfxShowMsg VARIANT$$(vPublicId)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the output 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
pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
IF ISNOTHING(pXslt) THEN EXIT FUNCTION
pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
TRY
pXslDoc.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
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the ownerDocument property.
' The following example uses the ownerDocument property to return the parent DOMDocument
' object, and then displays that object's root element tag name.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pDocElem1 AS IXMLDOMElement
LOCAL pDocElem2 AS IXMLDOMElement
LOCAL pChildNodes1 AS IXMLDOMNodeList
LOCAL pChildNodes2 AS IXMLDOMNodeList
LOCAL pItem1 AS IXMLDOMNode
LOCAL pItem2 AS IXMLDOMNode
LOCAL pCurrNode AS IXMLDOMNode
LOCAL pOwner AS IXMLDOMDocument
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pXmlDoc.async = %VARIANT_FALSE
IF ISTRUE pXmlDoc.load("books.xml") THEN
pDocElem1 = pXmlDoc.documentElement
pChildNodes1 = pDocElem1.childNodes
pItem1 = pChildNodes1.item(0)
pChildNodes2 = pItem1.childNodes
pItem2 = pChildNodes2.item(1)
pOwner = pItem2.ownerDocument
pDocElem2 = pOwner.documentElement
AfxShowMsg pDocElem2.tagName
END IF
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the ownerTemplate 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 pTemplate AS IXSLTemplate
LOCAL pStyleSheet AS IXMLDOMNode
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
IF ISNOTHING(pXslt) THEN EXIT FUNCTION
pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
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
pTemplate = pXslProc.ownerTemplate
pStyleSheet = pTemplate.stylesheet
AfxShowMsg pStyleSheet.xml
END IF
END IF
END FUNCTION
' ========================================================================================
' =========================================================================================
' Demonstrates the use of the parentNode property.
' The following example sets a variable ('pParentNode') to reference the parent node of
' another IXMLDOMNode object ('pChildNode'). It then uses the reference to the new node to
' display the XML contents of its parent 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 pCurrNode AS IXMLDOMNode
LOCAL pParentNode 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
pCurrNode = pRootNode.childNodes.item(1)
pParentNode = pCurrNode.childNodes.item(0).parentNode
AfxShowMsg pParentNode.xml
END IF
END FUNCTION
' =========================================================================================
' ========================================================================================
' Demonstrates the use of the parsed property.
' The following example displays whether the top-level node (root) and all its descendants
' are parsed.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pRootNode 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
pRootNode = pXmlDoc.documentElement
AfxShowMsg STR$(pRootNode.parsed)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the XMLDOMParseError interface.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pParseError AS IXMLDOMParseError
LOCAL bstrMsg AS STRING
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISFALSE ISOBJECT(pXmlDoc) THEN EXIT FUNCTION
IF ISFALSE pXmlDoc.load("bad.xml") THEN
pParseError = pXmlDoc.parseError
IF ISTRUE ISOBJECT(pParseError) THEN
bstrMsg = "-------------------------------------------------------------------------" & $CRLF
bstrMsg += "Error &H" & HEX$(pParseError.errorCode) & " in the xml file" & $CRLF
bstrMsg += "-------------------------------------------------------------------------" & $CRLF
bstrMsg += "Filepos: " & FORMAT$(pParseError.filePos) & $CRLF
bstrMsg += "Line: " & FORMAT$(pParseError.line) & $CRLF
bstrMsg += "Position: " & FORMAT$(pParseError.linePos) & $CRLF
bstrMsg += "Reason: " & pParseError.reason & $CRLF
bstrMsg += "Source text: " & pParseError.srcText & $CRLF
bstrMsg += "Url: " & pParseError.url & $CRLF
bstrMsg += "-------------------------------------------------------------------------" & $CRLF
pParseError = NOTHING
AfxShowMsg bstrMsg
END IF
END IF
pXmlDoc = NOTHING
END FUNCTION
' ========================================================================================
' =========================================================================================
' Demonstrates the use of the and peekNode 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
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")
pNodeBook = pSelection.peekNode
AfxShowMsg pNodeBook.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
' ========================================================================================
' Demonstrates the use of prefix property.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pParseError AS IXMLDOMParseError
LOCAL pRootNode AS IXMLDOMElement
LOCAL bstrNamespaceURI AS WSTRING
LOCAL i AS LONG
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.load "books2.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
' Get the namespace of the root element.
pRootNode = pXmlDoc.documentElement
bstrNamespaceURI = pRootNode.prefix
AfxShowMsg bstrNamespaceURI
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of preserveWhiteSpace property.
' The following example first loads and displays a file with the preserveWhiteSpace
' property set to True and then reloads and displays the file with the preserveWhiteSpace
' property set to False.
' ========================================================================================
#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.preserveWhiteSpace = %VARIANT_TRUE
pXmlDoc.load "books.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
AfxShowMsg pXmlDoc.xml
pXmlDoc.preserveWhiteSpace = %FALSE
pXmlDoc.load "books.xml"
AfxShowMsg pXmlDoc.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' =========================================================================================
' Demonstrates the use of the previousSibling property.
' The following example creates an IXMLDOMNode object and sets it to the previous sibling
' of the current 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 pCurrNode AS IXMLDOMNode
LOCAL pPrevNode 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)
pPrevNode = pCurrNode.previousSibling
AfxShowMsg pPrevNode.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
' ========================================================================================
' Demonstrates the use of the publicId property.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pEntity AS IXMLDOMEntity
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pDocType AS IXMLDOMDocumentType
LOCAL pDOMNode AS IXMLDOMNode
LOCAL vPublicId AS VARIANT
' 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 "doment1.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pDocType = pXmlDoc.doctype
pNamedNodeMap = pDocType.entities
pDOMNode = pNamedNodeMap.nextNode
pEntity = pDOMNode
vPublicID = pEntity.publicID
AfxShowMsg VARIANT$$(vPublicId)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the publicId 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 pEntity AS IXMLDOMEntity
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pDocType AS IXMLDOMDocumentType
LOCAL pDOMNode AS IXMLDOMNode
LOCAL vPublicId AS VARIANT
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
pDocType = pXmlDoc.doctype
pNamedNodeMap = pDocType.entities
pDOMNode = pNamedNodeMap.nextNode
pEntity = pDOMNode
vPublicID = pEntity.publicID
AfxShowMsg VARIANT$$(vPublicId)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the readyState (IXSLProcessor) property.
' Returns the current state of the processor.
' ========================================================================================
#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 iState AS LONG
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
IF ISNOTHING(pXslt) THEN EXIT FUNCTION
pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
pXslDoc.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
iState = pXslProc.readyState
AfxShowMsg "State = " & FORMAT$(iState)
pXslProc.input = pXmlDoc
iState = pXslProc.readyState
AfxShowMsg "State = " & FORMAT$(iState)
END IF
END IF
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the reason 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
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the removeAll method.
' ========================================================================================
#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.setProperty "SelectionLanguage", "XPath"
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.loadXML "<root><child/><child/><child/></root>"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
AfxShowMsg "Before removing <child> nodes: " & $CRLF & pXmlDoc.xml
pSelection = pXmlDoc.selectNodes("//child")
pSelection.removeAll
AfxShowMsg "After removing <child> nodes: " & $CRLF & pXmlDoc.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the removeAttribute 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 pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pDOMElement AS IXMLDOMElement
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
pNodeBook = pXmlDoc.selectSingleNode("//book")
pNamedNodeMap = pNodeBook.attributes
AfxShowMsg STR$(pNamedNodeMap.length)
pDOMElement = pNodeBook
pDOMElement.removeAttribute "id"
AfxShowMsg STR$(pNamedNodeMap.length)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the removeAttributeNode 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 pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pNodeId AS IXMLDOMAttribute
LOCAL pDOMElement AS IXMLDOMElement
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
pNodeBook = pXmlDoc.selectSingleNode("//book")
pNamedNodeMap = pNodeBook.attributes
AfxShowMsg STR$(pNamedNodeMap.length)
pDOMElement = pNodeBook
pNodeId = pDOMElement.getAttributeNode("id")
pDOMElement.removeAttributeNode pNodeId
AfxShowMsg STR$(pNamedNodeMap.length)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the removeChild method.
' The following example creates an IXMLDOMNode object (currNode), removes a child node
' from it, and displays the text of the removed 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 pCurrNode AS IXMLDOMNode
LOCAL pChildNode AS IXMLDOMNode
LOCAL pOldChild AS IXMLDOMNode
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
IF ISTRUE pXmlDoc.load("books.xml") THEN
pRootNode = pXmlDoc.documentElement
pCurrNode = pRootNode.childNodes.item(1)
pChildNode = pCurrNode.childNodes.item(1)
pOldChild = pCurrNode.removeChild(pChildNode)
AfxShowMsg pOldChild.text
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the removeNext 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 pDOMNode AS IXMLDOMNode
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")
DO
pDOMNode = pSelection.peekNode
IF ISNOTHING(pDOMNode) THEN EXIT DO
pDOMNode = pSelection.removeNext
LOOP
END IF
AfxShowMsg pXmlDoc.xml
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the removeQualifiedItem 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 pDOMNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pNodeId AS IXMLDOMNode
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
pNodeBook = pXmlDoc.selectSingleNode("//book")
pDOMNamedNodeMap = pNodeBook.attributes
AfxShowMsg STR$(pDOMNamedNodeMap.length)
pNodeId = pDOMNamedNodeMap.removeQualifiedItem("id", "")
AfxShowMsg STR$(pDOMNamedNodeMap.length)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the replaceChild 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 pNewElem AS IXMLDOMElement
LOCAL pChildNodes AS IXMLDOMNodeList
LOCAL pChildNodes2 AS IXMLDOMNodeList
LOCAL pItem1 AS IXMLDOMNode
LOCAL pItem2 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
pNewElem = pXmlDoc.createElement("PAGES")
pChildNodes = pRootNode.childNodes
pItem1 = pChildNodes.item(1)
pChildNodes2 = pItem1.childNodes
pItem2 = pChildNodes2.item(0)
pItem1.replaceChild pNewElem, pItem2
AfxShowMsg pItem1.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the replaceData method.
' The following example creates a new IXMLDOMComment object, and then replaces the first
' five characters with a new 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.load "books.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pComment = pXmlDoc.createComment("Hello World")
pComment.replaceData 0, 5, "Goodbye, Cruel"
AfxShowMsg pComment.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the reset method (IXMLDOMNamedNodeMap).
' ========================================================================================
#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
LOCAL pNodeAttribute AS IXMLDOMNode
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
pNodeBook = pXmlDoc.selectSingleNode("//book")
pNamedNodeMap = pNodeBook.attributes
pNamedNodeMap.reset
pNodeAttribute = pNamedNodeMap.nextNode
AfxShowMsg pNodeAttribute.text
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the reset method (IXMLDOMNodeList).
' The following script example creates an IXMLDOMNodeList object and iterates the
' collection using the nextNode method. It then uses the reset method to reset the
' iterator to point before the first node in the list.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pNodeList AS IXMLDOMNodeList
LOCAL pNode AS IXMLDOMNode
LOCAL bstrOut AS WSTRING
LOCAL i AS LONG
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
pNodeList = pXmlDoc.getElementsByTagName("author")
FOR i = 0 TO pNodeList.length - 1
bstrOut += pNodeList.nextNode.text & $CRLF
NEXT
AfxShowMsg bstrOut
pNodeList.reset
AfxShowMsg pNodeList.nextNode.text
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the reset method (IXSLProcessor).
' ========================================================================================
#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
pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
IF ISNOTHING(pXslt) THEN EXIT FUNCTION
pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
TRY
pXslDoc.async = %VARIANT_FALSE
pXslDoc.load "sample.xsl"
IF pXslDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXslDoc.parseError.reason
ELSE
pXslt.putref_stylesheet = pXslDoc
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
pXmlDoc.async = %VARIANT_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
vRes = pXslProc.output
AfxShowMsg VARIANT$$(vRes)
pXslProc.reset
vRes = pXslProc.output
AfxShowMsg VARIANT$$(vRes)
END IF
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the resolveExternals property.
' ========================================================================================
#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.resolveExternals = %VARIANT_FALSE
pXmlDoc.load "books.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
AfxShowMsg "Parse successful"
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' =========================================================================================
' Demonstrates the use of the save method.
' The following example creates a DomDocument object from a string, then saves the
' document to a file in the application folder. If you look at the resulting file you will
' see that, instead of one continuous line of text, after each tag or data string. That is
' because of the $LF constant inserted in the string at the appropriate locations.
' =========================================================================================
#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.validateOnParse = %VARIANT_FALSE
pXmlDoc.loadXML _
"<?xml version='1.0'?>" + $LF + _
"<doc title='test'>" + $LF + _
" <page num='1'>" + $LF + _
" <para title='Saved at last'>" + $LF + _
" This XML data is finally saved." + $LF + _
" </para>" + $LF + _
" </page>" + $LF + _
" <page num='2'>" + $LF + _
" <para>" + $LF + _
" This page is intentionally left blank." + $LF + _
" </para>" + $LF + _
" </page>" + $LF + _
"</doc>" + $LF
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pXmlDoc.save "saved.xml"
AfxShowMsg "Saved."
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
' ========================================================================================
' Demonstrates the use of the schemas property.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pSchemaCache AS IXMLDOMSchemaCollection2
pXmlDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pSchemaCache = NEWCOM "Msxml2.XMLSchemaCache.6.0"
IF ISNOTHING(pSchemaCache) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.validateOnParse = %VARIANT_FALSE
pSchemaCache.add "x-schema:books", "books.xsd"
pXmlDoc.putref_schemas = pSchemaCache
' The document will load only if a valid schema is attached to the xml file.
pXmlDoc.load "books.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
AfxShowMsg "Done."
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the selectNodes method.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pNodeList AS IXMLDOMNodeList
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
pNodeList = pXmlDoc.documentElement.selectNodes("//book")
AfxShowMsg STR$(pNodeList.length)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the selectNodes method.
' The following example creates an IXMLDOMNodeList object containing the nodes specified
' by the expression parameter (for example, all the <xsl:template> nodes in an XSLT style
' sheet). It then displays the number of nodes contained in the node list.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pNodeList AS IXMLDOMNodeList
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.load "hello.xsl"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pXmlDoc.setProperty "SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"
pXmlDOc.setProperty "SelectionLanguage", "XPath"
pNodeList = pXmlDoc.documentElement.selectNodes("//xsl:template")
AfxShowMsg FORMAT$(pNodeList.length)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of setProperty and selectSingleNode.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pNodeDescription AS IXMLDOMNode
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.setProperty "SelectionLanguage", "XPath"
pXmlDoc.load "books.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pNodeDescription = pXmlDoc.selectSingleNode("//description")
AfxShowMsg pNodeDescription.text
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the setAttribute method.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pDOMNode AS IXMLDOMNode
LOCAL pDOMElement AS IXMLDOMElement
LOCAL vValue 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
pDOMNode = pXmlDoc.selectSingleNode("//book")
pDOMELEment = pDOMNode
vValue = DATE$
pDOMElEment.setAttribute "PublishDate", vValue
vValue = pDOMElement.getAttribute("PublishDate")
AfxShowMsg VARIANT$$(vValue)
AfxShowMsg pDOMNode.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the setAttributeNode 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 pNodePublishDate AS IXMLDOMAttribute
LOCAL vValue 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
pNodePublishDate = pXmlDoc.createAttribute("PublishDate")
pNodePublishDate.nodeValue = DATE$
pNodeBook = pXmlDoc.selectSingleNode("//book")
pDOMELement = pNodeBook
pDOMElement.setAttributeNode pNodePublishDate
vValue = pDOMElement.getAttribute("PublishDate")
AfxShowMsg VARIANT$$(vValue)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' =========================================================================================
' Demonstrates the use of the setNamedItem method.
' =========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' =========================================================================================
' Main
' =========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pNodePublishDate AS IXMLDOMAttribute
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
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.async = %VARIANT_FALSE
pXmlDoc.load "books.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pNodePublishDate = pXmlDoc.createAttribute("PublishDate")
pNodePublishDate.value = DATE$
pNodeBook = pXmLDoc.selectSingleNode("//book")
pNamedNodeMap = pNodeBook.attributes
pNamedNodeMap.setNamedItem pNodePublishDate
pDOMElement = pNodeBook
vValue = pDOMElement.getAttribute("PublishDate")
AfxShowMsg VARIANT$$(vValue)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' =========================================================================================
[/code ]
' ========================================================================================
' Demonstrates the use of the setProperty 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 bstrOutput AS STRING
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")
bstrOutput = pSelection.expr
bstrOutput += " -- " & pSelection.item(0).xml
AfxShowMsg bstrOutput
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the setStartMode 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 pTemplate AS IXSLTemplate
LOCAL vRes AS VARIANT
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
IF ISNOTHING(pXslt) THEN EXIT FUNCTION
pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
TRY
pXslDoc.async = %VARIANT_FALSE
pXslDoc.load "sample2.xsl"
pXslt.putref_stylesheet = pXslDoc
pXmlDoc.async = %VARIANT_FALSE
IF ISTRUE pXmlDoc.load("books.xml") THEN
pXslProc = pXslt.createProcessor
pXslProc.input = pXmlDoc
pXslProc.setStartMode "View", ""
pXslProc.transform
vRes = pXslProc.output
AfxShowMsg VARIANT$$(vRes)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the specified property.
' The following example creates an IXMLDOMNode from the specified item in an
' IXMLDOMNamedNodeMap. It then displays whether or not the attribute was specified in the
' element, rather than in a DTD or schema.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pCurrNode AS IXMLDOMNode
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pMyNode 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.childNodes.item(0)
pNamedNodeMap = pCurrNode.attributes
pMyNode = pNamedNodeMap.item(0)
AfxShowMsg STR$(pMyNode.specified)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of spliText 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 pDOMNode AS IXMLDOMNode
LOCAL pNodeText AS IXMLDOMText
LOCAL pNewNodeText AS IXMLDOMText
LOCAL pNodeList AS IXMLDOMNodeList
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.loadXML "<root>Hello World!</root>"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
' Get a reference to the root node
pRootNode = pXmlDoc.documentElement
' Get a reference to the list of child nodes
pNodeList = pRootNode.childNodes
' Show how many nodes are in the list
AfxShowMsg STR$(pNodeList.length)
' Get a reference to the first child node
pDOMNode = pRootNode.firstChild
' Split the text
pNodeText = pDOMNode
pNewNodeText = pNodeText.splitText(6)
' Show how many nodes are in the list
AfxShowMsg STR$(pNodeList.length)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the srcText 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
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the startMode 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
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
IF ISNOTHING(pXslt) THEN EXIT FUNCTION
pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
TRY
pXslDoc.async = %VARIANT_FALSE
pXslDoc.load "sample2.xsl"
IF pXslDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXslDoc.parseError.reason
ELSE
pXslt.putref_stylesheet = pXslDoc
pXmlDoc.async = %VARIANT_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.setStartMode "view"
AfxShowMsg pXslProc.startMode
END IF
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the startModeURI property.
' By default, the starting mode for a new XSLT stylesheet is NULL and the startModeURI
' property returns the empty string ("") as its initial value.
' ========================================================================================
#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
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
IF ISNOTHING(pXslt) THEN EXIT FUNCTION
pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
TRY
pXslDoc.async = %VARIANT_FALSE
pXslDoc.load "sample2.xsl"
IF pXslDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXslDoc.parseError.reason
ELSE
pXslt.putref_stylesheet = pXslDoc
pXmlDoc.async = %VARIANT_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.setStartMode "view"
AfxShowMsg pXslProc.startModeURI
END IF
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the styleSheet property (IXSLProcessor).
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXslt AS IXSLTemplate
LOCAL pXslDoc AS IXMLDOMDocument
LOCAL pXslProc AS IXSLProcessor
pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
IF ISNOTHING(pXslt) THEN EXIT FUNCTION
pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
TRY
pXslDoc.async = %VARIANT_FALSE
pXslDoc.load "sample2.xsl"
IF pXslDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXslDoc.parseError.reason
ELSE
pXslt.putref_styleSheet = pXslDoc
pXslProc = pXslt.createProcessor
AfxShowMsg pXslProc.styleSheet.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the styleSheet property (IXSLTemplate).
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXslt AS IXSLTemplate
LOCAL pXslDoc AS IXMLDOMDocument
pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
IF ISNOTHING(pXslt) THEN EXIT FUNCTION
pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
TRY
pXslDoc.async = %VARIANT_FALSE
pXslDoc.load "sample2.xsl"
IF pXslDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXslDoc.parseError.reason
ELSE
pXslt.putref_stylesheet = pXslDoc
AfxShowMsg pXslt.stylesheet.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the substringData method.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pComment AS IXMLDOMComment
LOCAL MyStr AS WSTRING
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
pComment = pXmlDoc.createComment("Hello World!")
MyStr = pComment.substringData(0, 5)
AfxShowMsg MyStr
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the systemId property.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pEntity AS IXMLDOMEntity
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pDocType AS IXMLDOMDocumentType
LOCAL pDomNode AS IXMLDOMElement
LOCAL vsystemId AS VARIANT
' 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("doment1.xml")
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pDocType = pXmlDoc.doctype
pNamedNodeMap = pDocType.entities
pDOMNode = pNamedNodeMap.nextNode
pEntity = pDOMNode
vsystemId = pEntity.systemId
AfxShowMsg VARIANT$$(vsystemId)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the systemId 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 pDocType AS IXMLDOMDocumentType
LOCAL pDomNode AS IXMLDOMElement
LOCAL vsystemId AS VARIANT
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.setProperty "ProhibitDTD", %VARIANT_FALSE
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.load("doment1.xml")
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pDocType = pXmlDoc.doctype
pNamedNodeMap = pDocType.entities
pDOMNode = pNamedNodeMap.nextNode
pEntity = pDOMNode
vsystemId = pEntity.systemId
AfxShowMsg VARIANT$$(vsystemId)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the systemId property.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pNotation AS IXMLDOMNotation
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pDocType AS IXMLDOMDocumentType
LOCAL pDomNode AS IXMLDOMElement
LOCAL vsystemId AS VARIANT
' 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 "doment1.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pDocType = pXmlDoc.doctype
pNamedNodeMap = pDocType.notations
pDOMNode = pNamedNodeMap.nextNode
pNotation = pDOMNode
vsystemId = pNotation.systemId
AfxShowMsg VARIANT$$(vsystemId)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the systemId property.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pNotation AS IXMLDOMNotation
LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pDocType AS IXMLDOMDocumentType
LOCAL pDomNode AS IXMLDOMElement
LOCAL vsystemId AS VARIANT
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
pDocType = pXmlDoc.doctype
pNamedNodeMap = pDocType.notations
pDOMNode = pNamedNodeMap.nextNode
pNotation = pDOMNode
vsystemId = pNotation.systemId
AfxShowMsg VARIANT$$(vsystemId)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the tagName property.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pDOMNode AS IXMLDOMNode
LOCAL pOwner 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
pDOMNode = pXmlDoc.documentElement.childNodes.item(0)
pOwner = pDOMNode.childNodes.item(1).ownerDocument
AfxShowMsg pOwner.documentElement.tagName
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the tagName property.
' ========================================================================================
#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
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
AfxShowMsg pDOMElement.tagName
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the target property.
' The following example iterates through the document's child nodes. If it finds a node of
' type NODE_PROCESSING_INSTRUCTION (7), it displays the node's target.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pNodeList AS IXMLDOMNodeList
LOCAL pDOMNode AS IXMLDOMNode
LOCAL pPri AS IXMLDOMProcessingInstruction
LOCAL nodeType AS LONG
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
pNodeList = pXmlDoc.childNodes
FOR i = 0 TO pNodeList.length - 1
pDOMNode = pNodeList.item(i)
nodeType = pDOMNode.nodeType
IF nodeType = %NODE_PROCESSING_INSTRUCTION THEN
pPri = pDOMNode
AfxShowMsg pPri.target
END IF
NEXT
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the text property.
' ========================================================================================
#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.childNodes.item(0)
AfxShowMsg pCurrNode.text
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the transform 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 pTemplate AS IXSLTemplate
LOCAL vRes AS VARIANT
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
IF ISNOTHING(pXslt) THEN EXIT FUNCTION
pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION
TRY
pXslDoc.async = %VARIANT_FALSE
pXslDoc.load "sample2.xsl"
IF pXslDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXslDoc.parseError.reason
ELSE
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.transform
vRes = pXslProc.output
AfxShowMsg VARIANT$$(vRes)
END IF
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the transformNode method.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument
LOCAL pStyleSheet AS IXMLDOMDocument
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pStyleSheet = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pStyleSheet) 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
pStyleSheet.async = %VARIANT_FALSE
pStyleSheet.load "sample2.xsl"
IF pStyleSheet.parseError.errorCode THEN
AfxShowMsg "You have error " & pStyleSheet.parseError.reason
ELSE
AfxShowMsg pXmlDoc.transformnode(pStyleSheet)
END IF
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the transformNodeToObject method.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pSource AS IXMLDOMDocument
LOCAL pStylesheet AS IXMLDOMDocument
LOCAL pStylesheet2 AS IXMLDOMDocument
LOCAL pResult AS IXMLDOMDocument
LOCAL pResult2 AS IXMLDOMDocument
pSource = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pSource) THEN EXIT FUNCTION
pStylesheet = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pStylesheet) THEN EXIT FUNCTION
pStylesheet2 = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pStylesheet2) THEN EXIT FUNCTION
pResult = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pResult) THEN EXIT FUNCTION
pResult2 = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pResult2) THEN EXIT FUNCTION
TRY
pSource.async = %VARIANT_FALSE
pSource.load "sample.xml"
IF pSource.parseError.errorCode THEN
AfxShowMsg "You have error " & pSource.parseError.reason
ELSE
pStyleSheet.async = %VARIANT_FALSE
pStyleSheet.load "stylesheet1.xsl"
IF pStyleSheet.parseError.errorCode THEN
AfxShowMsg "You have error " & pStyleSheet.parseError.reason
ELSE
pResult.async = %VARIANT_FALSE
pResult.validateOnParse = %VARIANT_TRUE
pResult2.async = %VARIANT_FALSE
pResult2.validateOnParse = %VARIANT_TRUE
pSource.transformNodeToObject pStylesheet, pResult
pStyleSheet2.async = %VARIANT_FALSE
pStyleSheet2.load "stylesheet2.xsl"
IF pStyleSheet2.parseError.errorCode THEN
AfxShowMsg "You have error " & pStyleSheet2.parseError.reason
ELSE
pResult.transformNodeToObject pStylesheet2, pResult2
AfxShowMsg pResult2.xml
END IF
END IF
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the url property (IXMLDOMDocument).
' ========================================================================================
#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 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
AfxShowMsg pXmlDoc.url
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the validate method.
' When used with MSXML 6.0, you should get the following output:
' Invalid Dom: Element 'review' is unexpected according to content model of parent
' element 'book'
' Expecting: price.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL i AS LONG
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pParseError AS IXMLDOMParseError
LOCAL pSchemaCache AS IXMLDOMSchemaCollection
LOCAL pNodeList AS IXMLDOMNodeList
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pSchemaCache = NEWCOM "Msxml2.XMLSchemaCache.6.0"
IF ISNOTHING(pSchemaCache) THEN EXIT FUNCTION
pSchemaCache.add "urn:books", "validateNode.xsd"
pXmlDoc.putref_schemas = pSchemaCache
pXmlDoc.validateOnParse = %VARIANT_FALSE
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.load "validateNode.xml"
pParseError = pXmlDoc.validate
IF OBJRESULT = %S_FALSE THEN
AfxShowMsg "Invalid DOM - Error &H: " & HEX$(pParseError.errorCode, 8)
AfxShowMsg pParseError.reason
ELSEIF OBJRESULT = %E_PENDING THEN
AfxShowMsg "The document is not completely loaded."
ELSEIF OBJRESULT <> %S_OK THEN
AfxShowMsg "Error &H" & HEX$(OBJRESULT)
ELSE
AfxShowMsg "DOM is valid: " & pXmlDoc.xml
pNodeList = pXmlDoc.selectNodes("//book")
FOR i = 0 TO pNodeList.length - 1
AfxShowMsg pNodeList.item(i).xml
NEXT
END IF
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the validateNode method.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument3
LOCAL pParseError AS IXMLDOMParseError
LOCAL pSchemaCache AS IXMLDOMSchemaCollection
LOCAL pNodeList AS IXMLDOMNodeList
LOCAL pNode AS IXMLDOMNode
LOCAL i AS LONG
pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
pSchemaCache = NEWCOM "Msxml2.XMLSchemaCache.6.0"
IF ISNOTHING(pSchemaCache) THEN EXIT FUNCTION
pSchemaCache.add "urn:books", "validateNode.xsd"
pXmlDoc.putref_schemas = pSchemaCache
pXmlDoc.validateOnParse = %VARIANT_FALSE
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.load "validateNode.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pNodeList = pXmlDoc.selectNodes("//book")
FOR i = 0 TO pNodeList.length - 1
pNode = pNodeList.item(i)
pParseError = pXmlDoc.validateNode(pNode)
IF OBJRESULT = %S_FALSE THEN
AfxShowMsg "Invalid node - Error &H" & HEX$(pParseError.errorCode, 8)
AfxShowMsg pParseError.reason
ELSEIF OBJRESULT <> %S_OK THEN
AfxShowMsg "Error &H" & HEX$(OBJRESULT)
ELSE
AfxShowMsg pNode.xml
END IF
pParseError = NOTHING
pNode = NOTHING
NEXT
END IF
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the validateOnParse property.
' ========================================================================================
#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 ISFALSE ISOBJECT(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.validateOnParse = %VARIANT_TRUE
pXmlDoc.load "books.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
AfxShowMsg "Validated"
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the value property.
' The following example gets the value of the first attribute of the document root and
' assigns it to the variable vValue.
' ========================================================================================
#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 pNamedNodeMap AS IXMLDOMNamedNodeMap
LOCAL pDOMNode AS IXMLDOMNode
LOCAL pDOMAttr AS IXMLDOMAttribute
LOCAL vValue 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
pRootNode = pXmlDoc.documentElement
pCurrNode = pRootNode.firstChild
pNamedNodeMap = pXmlDoc.documentElement.firstChild.attributes
pDOMNode = pNamedNodeMap.item(0)
pDOMAttr = pDOMNode
vValue = pDOMAttr.value
AfxShowMsg VARIANT$$(vValue)
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
' ========================================================================================
' Demonstrates the use of the xml property.
' ========================================================================================
#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.childNodes.Item(0)
AfxShowMsg pCurrNode.xml
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
The XPath implementation in MSXML 4.0 introduces a set of XSD extension functions that can specify expressions that evaluate nodes in an XML document based on their data type. The ms:type-local-name XPath extension function can return the non-qualified name of a node's data type as defined in an associated XSD schema.
' ========================================================================================
' Demonstrates the use of the ms:type-local-name XPath Extension function when
' programming the MSXML DOM.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pParseError AS IXMLDOMParseError
LOCAL pNodeList AS IXMLDOMNodeList
LOCAL pNode AS IXMLDOMNode
LOCAL i AS LONG
' only works with version 4.0
pXmlDoc = NEWCOM "Msxml2.DOMDocument.4.0"
IF ISFALSE ISOBJECT(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.setProperty "SelectionNamespaces", "xmlns:ms='urn:schemas-microsoft-com:xslt'"
pXmlDoc.load "books3.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pNodeList = pXmlDoc.selectNodes("//*[ms:type-local-name(.)='AuthorType']")
FOR i = 0 TO pNodeList.length - 1
AfxShowMsg pNodeList.item(i).nodeName
NEXT
pNodeList = NOTHING
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
The following example uses an XSLT template rule to select from books.xml all the elements and to output the elements data types and the namesapce URI as defined in books.xsd.
' ========================================================================================
' Demostrates the use of the ms:type-namespace-uri XPath Extension function.
' The following example uses an XSLT template rule to select from books.xml all the
' elements and to output the elements data types and the namesapce URI as defined in
' books.xsd.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pxsd AS IXMLDOMSchemaCollection
LOCAL pxml AS IXMLDOMDocument2
LOCAL pxsl AS IXMLDOMDocument2
pxsd = NEWCOM "Msxml2.XMLSchemaCache.6.0"
IF ISNOTHING(pxsd) THEN EXIT FUNCTION
pxml = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pxml) THEN EXIT FUNCTION
pxsl = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pxsl) THEN EXIT FUNCTION
' namespace uri ("urn:books") must be declared as one of the namespace
' declarations in the "books.xml" that is an instance of "books.xsd"
pxsd.add "urn:books", "books5.xsd"
pxml.putref_schemas = pxsd
pxml.setProperty "SelectionLanguage", "XPath'"
pxml.setProperty "SelectionNamespaces", "xmlns:ms='urn:schemas-microsoft-com:xslt'"
pxml.async = %VARIANT_FALSE
pxml.validateOnParse = %VARIANT_TRUE
pxml.load "books5.xml"
pxsl.async = %VARIANT_FALSE
pxsl.load "books5.xslt"
AfxShowMsg pxml.transformNode(pxsl)
END FUNCTION
' ========================================================================================
The XPath implementation in MSXML version 4.0 introduces a set of XML Schema Definition (XSD) extension functions that can specify expressions that evaluate nodes in an XML document based on the data type of the nodes. The ms:type-namespace-uri XPath extension function can return the namespace Uniform Resource Identifier (URI) that is associated with the XSD data type of the current node, or the first node in the node set that is provided as its input parameter. This function can run XPath queries in MSXML 4.0 DOM code to identify nodes whose data types are associated with a specified namespace URI. This article provides a code sample to demonstrate how to use the ms:type-namespace-uri XPath extension function when you program the MSXML 4.0 DOM in PowerBASIC.
' ========================================================================================
' Demonstrates the use of the ms:type-namespace-uri XPath Extension function when
' programming the MSXML DOM.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pXmlDoc AS IXMLDOMDocument2
LOCAL pParseError AS IXMLDOMParseError
LOCAL pNodeList AS IXMLDOMNodeList
LOCAL i AS LONG
' only works with version 4.0
pXmlDoc = NEWCOM "Msxml2.DOMDocument.4.0"
IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
TRY
pXmlDoc.async = %VARIANT_FALSE
pXmlDoc.setProperty "SelectionNamespaces", "xmlns:ms='urn:schemas-microsoft-com:xslt'"
pXmlDoc.load "books3.xml"
IF pXmlDoc.parseError.errorCode THEN
AfxShowMsg "You have error " & pXmlDoc.parseError.reason
ELSE
pNodeList = pXmlDoc.selectNodes("//*[ms:type-namespace-uri()='urn:books']")
FOR i = 0 TO pNodeList.length - 1
AfxShowMsg pNodeList.item(i).nodeName
NEXT
pNodeList = NOTHING
END IF
CATCH
AfxShowMsg OleGetErrorInfo(OBJRESULT)
END TRY
END FUNCTION
' ========================================================================================
The following example uses an XSLT template rule to select from books.xml all the elements and to output the elements data types and the namesapce URI as defined in books.xsd.
' ========================================================================================
' Demonstrates the use of the ms:type-namespace-uri XPath Extension function.
' The following example uses an XSLT template rule to select from books.xml all the
' elements and to output the elements data types and the namesapce URI as defined in
' books.xsd.
' ========================================================================================
#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL pxsd AS IXMLDOMSchemaCollection
LOCAL pxml AS IXMLDOMDocument2
LOCAL pxsl AS IXMLDOMDocument2
pxsd = NEWCOM "Msxml2.XMLSchemaCache.6.0"
IF ISNOTHING(pxsd) THEN EXIT FUNCTION
pxml = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pxml) THEN EXIT FUNCTION
pxsl = NEWCOM "Msxml2.DOMDocument.6.0"
IF ISNOTHING(pxsl) THEN EXIT FUNCTION
' namespace uri ("urn:books") must be declared as one of the namespace
' declarations in the "books.xml" that is an instance of "books.xsd"
pxsd.add "urn:books", "books4.xsd"
pxml.putref_schemas = pxsd
pxml.setProperty "SelectionLanguage", "XPath'"
pxml.setProperty "SelectionNamespaces", "xmlns:ms='urn:schemas-microsoft-com:xslt'"
pxml.async = %FALSE
pxml.validateOnParse = %TRUE
pxml.load "books4.xml"
pxsl.async = %FALSE
pxsl.load "books4.xslt"
AfxShowMsg pxml.transformNode(pxsl)
END FUNCTION
' ========================================================================================