Sample code for 30+ languages & platforms
VBScript

XML Add Child Tree

Demonstrates the Xml.AddChildTree method.

Chilkat VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

' 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>

set xml = CreateObject("Chilkat.Xml")
xml.Tag = "soap12:Envelope"
success = xml.AddAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
success = xml.AddAttribute("xmlns:xsd","http://www.w3.org/2001/XMLSchema")
success = xml.AddAttribute("xmlns:soap12","http://www.w3.org/2003/05/soap-envelope")
success = xml.UpdateAttrAt("soap12:Body|cteDadosMsg",1,"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.

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

' Add the child tree at the desired location:
' Temporarily point to the location where we wish to add the child tree.
success = xml.FindChild2("soap12:Body|cteDadosMsg")
If (success = 0) Then
    ' Restore current location to the root.
    xml.GetRoot2 
    WScript.Quit
End If

success = xml.AddChildTree(xml2)
' Restore the current location to the root.
xml.GetRoot2 

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

outFile.WriteLine(xml.GetXml())

' <?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>

outFile.Close