Sample code for 30+ languages & platforms
Unicode C++

XML Add Child Tree

Demonstrates the Xml.AddChildTree method.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkXmlW.h>

void ChilkatSample(void)
    {
    bool success = 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>

    CkXmlW xml;
    xml.put_Tag(L"soap12:Envelope");
    xml.AddAttribute(L"xmlns:xsi",L"http://www.w3.org/2001/XMLSchema-instance");
    xml.AddAttribute(L"xmlns:xsd",L"http://www.w3.org/2001/XMLSchema");
    xml.AddAttribute(L"xmlns:soap12",L"http://www.w3.org/2003/05/soap-envelope");
    xml.UpdateAttrAt(L"soap12:Body|cteDadosMsg",true,L"xmlns",L"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.

    CkXmlW xml2;
    xml2.put_Tag(L"consStatServCTe");
    xml2.AddAttribute(L"versao",L"4.00");
    xml2.AddAttribute(L"xmlns",L"http://www.portalfiscal.inf.br/cte");
    xml2.UpdateChildContent(L"tpAmb",L"2");
    xml2.UpdateChildContent(L"cUF",L"43");
    xml2.UpdateChildContent(L"xServ",L"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(L"soap12:Body|cteDadosMsg");
    if (success == false) {
        //  Restore current location to the root.
        xml.GetRoot2();
        return;
    }

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

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

    wprintf(L"%s\n",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>
    }