AutoIt with XML: Add a child/grandchild node or remove any node

Sometimes, we have to use AutoIt script to edit an xml, add a node or remove a node, to make some deployment documents fitable to some project.

I have picked up a piece of script function about it as below:

Func append_node($SourceFile)
    $objDom = ObjCreate("Microsoft.XMLDOM")
    $objDom.load($SourceFile)
    $objRoot = $objDom.documentElement.selectSingleNode('//Left')

    ;Add a child node
    $child1 = $objDom.createElement("rootElement")
    $child1.text = "Child1"
    $child1.setAttribute("Att1Child1","Child1_TextAtt1")
    $objRoot.appendChild($child1)

    ;Add a grandchild node
    $objChild1 = $objDom.createElement("childElement1")
    $objChild1.text = "objChild1"
    $objChild1.setAttribute("Att1ObjChild1","obj_Child1_TextAtt1")
    $child1.appendChild($objChild1)

    ;Add a grandchild node
    $objChild2 = $objDom.createElement("childElement2")
    $objChild2.text = "objChild2"
    $objChild2.setAttribute("Att2ObjChild2","obj_Child2_TextAtt21")
    $child1.appendChild($objChild2)

    $objDom.save($SourceFile)
EndFunc

Func remove_node($SourceFile)
    $oXML = ObjCreate("Microsoft.XMLDOM")
    $oXML.Load($SourceFile)
    $oNode = $oXML.documentElement.selectSingleNode('//Left')
    ;$oNode.parentNode.removeChild($oNode)
    $remove_node = $oXML.documentElement.selectSingleNode('//Left/NewChild1')
    ;Remove the child node
    $oNode.removeChild($remove_node)
    $oXML.save($SourceFile)
EndFunc


$SourceFile = "STRPControl.xml"
append_node($SourceFile)
posted @ 2013-12-20 15:37  chenpassion  阅读(880)  评论(0编辑  收藏  举报