Sample code for 30+ languages & platforms
PureBasic

XML Add Child Tree

Demonstrates the Xml.AddChildTree method.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkXml.pb"

Procedure ChilkatExample()

    success.i = 0

    ; 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.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml, "soap12:Envelope")
    CkXml::ckAddAttribute(xml,"xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
    CkXml::ckAddAttribute(xml,"xmlns:xsd","http://www.w3.org/2001/XMLSchema")
    CkXml::ckAddAttribute(xml,"xmlns:soap12","http://www.w3.org/2003/05/soap-envelope")
    CkXml::ckUpdateAttrAt(xml,"soap12:Body|cteDadosMsg",1,"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.i = CkXml::ckCreate()
    If xml2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml2, "consStatServCTe")
    CkXml::ckAddAttribute(xml2,"versao","4.00")
    CkXml::ckAddAttribute(xml2,"xmlns","http://www.portalfiscal.inf.br/cte")
    CkXml::ckUpdateChildContent(xml2,"tpAmb","2")
    CkXml::ckUpdateChildContent(xml2,"cUF","43")
    CkXml::ckUpdateChildContent(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::ckFindChild2(xml,"soap12:Body|cteDadosMsg")
    If success = 0
        ; Restore current location to the root.
        CkXml::ckGetRoot2(xml)
        CkXml::ckDispose(xml)
        CkXml::ckDispose(xml2)
        ProcedureReturn
    EndIf

    success = CkXml::ckAddChildTree(xml,xml2)
    ; Restore the current location to the root.
    CkXml::ckGetRoot2(xml)

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

    Debug CkXml::ckGetXml(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::ckDispose(xml)
    CkXml::ckDispose(xml2)


    ProcedureReturn
EndProcedure