Friday, April 25, 2014

Create an XML Document Node with assoticated Elements and Attributes


' Create an XML Document Node with assoticated Elements and Attributes
'
'  Parameters:
'      oXMLDoc - XML DOM object ( Created from MSXML2.DOMDocument )
'  oTargetNode - XML Document Element where the element is created
'    sNodeName - Name of the Element Created
'  oAttributes - VBScript Dictionary object containing a list of XML Attributes to add
'    oElements - VBScript Dictionary object containing a list of XML Elements to add
'
'  Example Usage:
'   dim     xmlDoc, oAttributes, oElements
'   set  oAttributes = CreateObject("Scripting.Dictionary")
'   set    oElements = CreateObject("Scripting.Dictionary")
'      Set       xmlDoc =  CreateObject ("Microsoft.XMLDOM")
'   xmlDoc.Load "c:\SomeFile.xml"     or   xmlDoc.appendChild xmlDoc.createElement("MyRootElement")
'
'   oAttributes.Add "guid", left( CreateObject("Scriptlet.TypeLib").GUID, 38 ) ' Strip trailing NULL's
'     oElements.Add "Name","BuildName.value"
'     oElements.Add "Version","1.0"
'
'   CreateXMLNode xmlDoc, xmlDoc.documentElement, "MyElement", oAttributes, oElements
'
'   xmlDoc.Save "c:\SomeFile.xml"
'

Function CreateXMLNode ( xmlDoc, oTargetNode, sNodeName, oAttributes , oElements)

    Dim Key, oElement, oNodeRoot

    Set oNodeRoot = xmlDoc.createElement(sNodeName)

    For each key in oAttributes
        If IsEmpty(key) then
            Exit For
        End if
        oNodeRoot.setAttribute key, oAttributes.Item(key)
    Next

    For each key in oElements.Keys
        If isempty(key) then exit for
        Set oElement = xmlDoc.createElement(key)
        oElement.text = oElements.Item(key)
        oNodeRoot.appendChild oElement
    Next

    oTargetNode.appendChild oNodeRoot

End function

No comments:

Post a Comment