Sample code for 30+ languages & platforms
C

XML Add Child Tree

Demonstrates the Xml.AddChildTree method.

Chilkat C Downloads

C
#include <C_CkXml.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXml xml;
    HCkXml 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 = CkXml_Create();
    CkXml_putTag(xml,"soap12:Envelope");
    CkXml_AddAttribute(xml,"xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
    CkXml_AddAttribute(xml,"xmlns:xsd","http://www.w3.org/2001/XMLSchema");
    CkXml_AddAttribute(xml,"xmlns:soap12","http://www.w3.org/2003/05/soap-envelope");
    CkXml_UpdateAttrAt(xml,"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.

    xml2 = CkXml_Create();
    CkXml_putTag(xml2,"consStatServCTe");
    CkXml_AddAttribute(xml2,"versao","4.00");
    CkXml_AddAttribute(xml2,"xmlns","http://www.portalfiscal.inf.br/cte");
    CkXml_UpdateChildContent(xml2,"tpAmb","2");
    CkXml_UpdateChildContent(xml2,"cUF","43");
    CkXml_UpdateChildContent(xml2,"xServ","STATUS");

    //  Add the child tree at the desired location:
    //  Temporarily point to the location where we wish to add the child tree.
    success = CkXml_FindChild2(xml,"soap12:Body|cteDadosMsg");
    if (success == FALSE) {
        //  Restore current location to the root.
        CkXml_GetRoot2(xml);
        CkXml_Dispose(xml);
        CkXml_Dispose(xml2);
        return;
    }

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

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

    printf("%s\n",CkXml_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>


    CkXml_Dispose(xml);
    CkXml_Dispose(xml2);

    }