Sample code for 30+ languages & platforms
PowerBuilder

XML Add Child Tree

Demonstrates the Xml.AddChildTree method.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Xml
oleobject loo_Xml2

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

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
    destroy loo_Xml
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Xml.Tag = "soap12:Envelope"
loo_Xml.AddAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
loo_Xml.AddAttribute("xmlns:xsd","http://www.w3.org/2001/XMLSchema")
loo_Xml.AddAttribute("xmlns:soap12","http://www.w3.org/2003/05/soap-envelope")
loo_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.

loo_Xml2 = create oleobject
li_rc = loo_Xml2.ConnectToNewObject("Chilkat.Xml")

loo_Xml2.Tag = "consStatServCTe"
loo_Xml2.AddAttribute("versao","4.00")
loo_Xml2.AddAttribute("xmlns","http://www.portalfiscal.inf.br/cte")
loo_Xml2.UpdateChildContent("tpAmb","2")
loo_Xml2.UpdateChildContent("cUF","43")
loo_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.
li_Success = loo_Xml.FindChild2("soap12:Body|cteDadosMsg")
if li_Success = 0 then
    //  Restore current location to the root.
    loo_Xml.GetRoot2()
    destroy loo_Xml
    destroy loo_Xml2
    return
end if

li_Success = loo_Xml.AddChildTree(loo_Xml2)
//  Restore the current location to the root.
loo_Xml.GetRoot2()

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

Write-Debug loo_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>


destroy loo_Xml
destroy loo_Xml2