Sample code for 30+ languages & platforms
Rust

XML Add Child Tree

Demonstrates the Xml.AddChildTree method.

Chilkat Rust Downloads

Rust

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

let xml = chilkat::Xml::new();
xml.set_tag("soap12:Envelope");
let _ = xml.add_attribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
let _ = xml.add_attribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
let _ = xml.add_attribute("xmlns:soap12", "http://www.w3.org/2003/05/soap-envelope");
let _ = xml.update_attr_at("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.

let xml2 = chilkat::Xml::new();
xml2.set_tag("consStatServCTe");
let _ = xml2.add_attribute("versao", "4.00");
let _ = xml2.add_attribute("xmlns", "http://www.portalfiscal.inf.br/cte");
xml2.update_child_content("tpAmb", "2");
xml2.update_child_content("cUF", "43");
xml2.update_child_content("xServ", "STATUS");

// Add the child tree at the desired location:
// Temporarily point to the location where we wish to add the child tree.
if xml.find_child2("soap12:Body|cteDadosMsg").is_err() {
    // Restore current location to the root.
    xml.get_root2();
    return;
}

let _ = xml.add_child_tree(&xml2).is_ok();
// Restore the current location to the root.
xml.get_root2();

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

println!("{}", xml.get_xml().unwrap_or_default());

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