Sample code for 30+ languages & platforms
AutoIt

XML Add Child Tree

Demonstrates the Xml.AddChildTree method.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Build the following XML:

;  <?xml version="1.0" encoding="utf-8"?>
;  <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
;      <soap12:Body>
;          <cteDadosMsg xmlns="http://www.portalfiscal.inf.br/cte/wsdl/CTeStatusServicoV4">
;  	   </cteDadosMsg>
;      </soap12:Body>
;  </soap12:Envelope>

$oXml = ObjCreate("Chilkat.Xml")
$oXml.Tag = "soap12:Envelope"
$oXml.AddAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
$oXml.AddAttribute("xmlns:xsd","http://www.w3.org/2001/XMLSchema")
$oXml.AddAttribute("xmlns:soap12","http://www.w3.org/2003/05/soap-envelope")
$oXml.UpdateAttrAt("soap12:Body|cteDadosMsg",True,"xmlns","http://www.portalfiscal.inf.br/cte/wsdl/CTeStatusServicoV4")

;  We want to add the following XML as a sub-tree under the "cteDadosMsg" element.

;  <consStatServCTe versao="4.00" xmlns="http://www.portalfiscal.inf.br/cte">
;      <tpAmb>2</tpAmb>
;      <cUF>43</cUF>
;      <xServ>STATUS</xServ>
;  </consStatServCTe>

;  Build the XML to be added as a child tree.

$oXml2 = ObjCreate("Chilkat.Xml")
$oXml2.Tag = "consStatServCTe"
$oXml2.AddAttribute("versao","4.00")
$oXml2.AddAttribute("xmlns","http://www.portalfiscal.inf.br/cte")
$oXml2.UpdateChildContent "tpAmb","2"
$oXml2.UpdateChildContent "cUF","43"
$oXml2.UpdateChildContent "xServ","STATUS"

;  Add the child tree at the desired location:
;  Temporarily point to the location where we wish to add the child tree.
$bSuccess = $oXml.FindChild2("soap12:Body|cteDadosMsg")
If ($bSuccess = False) Then
    ;  Restore current location to the root.
    $oXml.GetRoot2 
    Exit
EndIf

$bSuccess = $oXml.AddChildTree($oXml2)
;  Restore the current location to the root.
$oXml.GetRoot2 

;  You can see now that the XML contain the child tree:

ConsoleWrite($oXml.GetXml() & @CRLF)

;  <?xml version="1.0" encoding="utf-8"?>
;  <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
;      <soap12:Body>
;          <cteDadosMsg xmlns="http://www.portalfiscal.inf.br/cte/wsdl/CTeStatusServicoV4">
;              <consStatServCTe versao="4.00" xmlns="http://www.portalfiscal.inf.br/cte">
;                  <tpAmb>2</tpAmb>
;                  <cUF>43</cUF>
;                  <xServ>STATUS</xServ>
;              </consStatServCTe>
;          </cteDadosMsg>
;      </soap12:Body>
;  </soap12:Envelope>