Sample code for 30+ languages & platforms
DataFlex

XML Add Child Tree

Demonstrates the Xml.AddChildTree method.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoXml
    Variant vXml2
    Handle hoXml2
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "soap12:Envelope"
    Get ComAddAttribute Of hoXml "xmlns:xsi" "http://www.w3.org/2001/XMLSchema-instance" To iSuccess
    Get ComAddAttribute Of hoXml "xmlns:xsd" "http://www.w3.org/2001/XMLSchema" To iSuccess
    Get ComAddAttribute Of hoXml "xmlns:soap12" "http://www.w3.org/2003/05/soap-envelope" To iSuccess
    Get ComUpdateAttrAt Of hoXml "soap12:Body|cteDadosMsg" True "xmlns" "http://www.portalfiscal.inf.br/cte/wsdl/CTeStatusServicoV4" To iSuccess

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

    Get Create (RefClass(cComChilkatXml)) To hoXml2
    If (Not(IsComObjectCreated(hoXml2))) Begin
        Send CreateComObject of hoXml2
    End
    Set ComTag Of hoXml2 To "consStatServCTe"
    Get ComAddAttribute Of hoXml2 "versao" "4.00" To iSuccess
    Get ComAddAttribute Of hoXml2 "xmlns" "http://www.portalfiscal.inf.br/cte" To iSuccess
    Send ComUpdateChildContent To hoXml2 "tpAmb" "2"
    Send ComUpdateChildContent To hoXml2 "cUF" "43"
    Send ComUpdateChildContent To hoXml2 "xServ" "STATUS"

    // Add the child tree at the desired location:
    // Temporarily point to the location where we wish to add the child tree.
    Get ComFindChild2 Of hoXml "soap12:Body|cteDadosMsg" To iSuccess
    If (iSuccess = False) Begin
        // Restore current location to the root.
        Send ComGetRoot2 To hoXml
        Procedure_Return
    End

    Get pvComObject of hoXml2 to vXml2
    Get ComAddChildTree Of hoXml vXml2 To iSuccess
    // Restore the current location to the root.
    Send ComGetRoot2 To hoXml

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

    Get ComGetXml Of hoXml To sTemp1
    Showln sTemp1

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


End_Procedure