Sample code for 30+ languages & platforms
Unicode C

XML Add Child Tree

Demonstrates the Xml.AddChildTree method.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkXmlW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlW xml;
    HCkXmlW xml2;

    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>

    xml = CkXmlW_Create();
    CkXmlW_putTag(xml,L"soap12:Envelope");
    CkXmlW_AddAttribute(xml,L"xmlns:xsi",L"http://www.w3.org/2001/XMLSchema-instance");
    CkXmlW_AddAttribute(xml,L"xmlns:xsd",L"http://www.w3.org/2001/XMLSchema");
    CkXmlW_AddAttribute(xml,L"xmlns:soap12",L"http://www.w3.org/2003/05/soap-envelope");
    CkXmlW_UpdateAttrAt(xml,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.

    xml2 = CkXmlW_Create();
    CkXmlW_putTag(xml2,L"consStatServCTe");
    CkXmlW_AddAttribute(xml2,L"versao",L"4.00");
    CkXmlW_AddAttribute(xml2,L"xmlns",L"http://www.portalfiscal.inf.br/cte");
    CkXmlW_UpdateChildContent(xml2,L"tpAmb",L"2");
    CkXmlW_UpdateChildContent(xml2,L"cUF",L"43");
    CkXmlW_UpdateChildContent(xml2,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 = CkXmlW_FindChild2(xml,L"soap12:Body|cteDadosMsg");
    if (success == FALSE) {
        //  Restore current location to the root.
        CkXmlW_GetRoot2(xml);
        CkXmlW_Dispose(xml);
        CkXmlW_Dispose(xml2);
        return;
    }

    success = CkXmlW_AddChildTree(xml,xml2);
    //  Restore the current location to the root.
    CkXmlW_GetRoot2(xml);

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

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


    CkXmlW_Dispose(xml);
    CkXmlW_Dispose(xml2);

    }