Sample code for 30+ languages & platforms
Zig

XML Add Child Tree

Demonstrates the Xml.AddChildTree method.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("soap12:Envelope");
    xml.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") catch {};
    xml.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema") catch {};
    xml.addAttribute("xmlns:soap12", "http://www.w3.org/2003/05/soap-envelope") catch {};
    xml.updateAttrAt("soap12:Body|cteDadosMsg", true, "xmlns", "http://www.portalfiscal.inf.br/cte/wsdl/CTeStatusServicoV4") catch {};

    // 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.

    const xml2 = try chilkat.Xml.init();
    defer xml2.deinit();
    xml2.setTag("consStatServCTe");
    xml2.addAttribute("versao", "4.00") catch {};
    xml2.addAttribute("xmlns", "http://www.portalfiscal.inf.br/cte") catch {};
    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.
    xml.findChild2("soap12:Body|cteDadosMsg") catch {
        // Restore current location to the root.
        xml.getRoot2();
        return;
    };

    try xml.addChildTree(xml2);
    // Restore the current location to the root.
    xml.getRoot2();

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

    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // <?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>
}