Sample code for 30+ languages & platforms
Unicode C++

Move XML Subtree to Another XML Document

Demonstrates using the InsertChildTreeBefore method to move a fragment of XML from one document to another.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkXmlW.h>

void ChilkatSample(void)
    {
    // Source XML is this:

    // <?xml version='1.0' encoding='UTF-8'?>
    // <soapenv:Envelope xmlns:"soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    //               <soapenv:Header/>
    //               <soapenv:Body>
    //                             <GetCustomerResponse xmlns="http://www.midoco.de/crm" xmlns:tns="http://www.midoco.de/ws">
    //                                           <CrmCustomer addresseeLine1="Max Mustermann" addresseeLine2="" changingUser="123456" >
    //                                                          <CrmAddress addressId="2225355" addressTypeId="1" checkStatus="O" city="Wien" countryCode="AT" customerId="000071"/>
    //                                                          <CrmPerson birthDay="30" birthMonth="8" birthYear="1977" birthday="1977-08-30T00:00:00.000+02:00" birthdayNotProvided="false"/>
    //                                           </CrmCustomer>
    //                           </GetCustomerResponse>
    //               </soapenv:Body>
    // </soapenv:Envelope>

    // Build the source XML.
    CkXmlW srcXml;
    srcXml.put_Tag(L"soapenv:Envelope");
    srcXml.AddAttribute(L"xmlns:soapenv",L"http://schemas.xmlsoap.org/soap/envelope/");
    srcXml.UpdateChildContent(L"soapenv:Header",L"");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse",true,L"xmlns",L"http://www.midoco.de/crm");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse",true,L"xmlns:tns",L"http://www.midoco.de/ws");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer",true,L"addresseeLine1",L"Max Mustermann");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer",true,L"addresseeLine2",L"");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer",true,L"changingUser",L"123456");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress",true,L"addressId",L"2225355");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress",true,L"addressTypeId",L"1");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress",true,L"checkStatus",L"O");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress",true,L"city",L"Wien");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress",true,L"countryCode",L"AT");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress",true,L"customerId",L"000071");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson",true,L"birthDay",L"30");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson",true,L"birthMonth",L"8");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson",true,L"birthYear",L"1977");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson",true,L"birthday",L"1977-08-30T00:00:00.000+02:00");
    srcXml.UpdateAttrAt(L"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson",true,L"birthdayNotProvided",L"false");

    // Destination XML is this:

    // <?xml version="1.0" encoding="utf-8"?>
    // <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    //     <SOAP-ENV:Header>
    //         <m:Credentials xmlns:m="http://www.midoco.de/system">
    //             <m:Login>User</m:Login>
    //             <m:Password>Pass</m:Password>
    //             <m:OrgUnit>ABC</m:OrgUnit>
    //             <m:Locale>de_DE</m:Locale>
    //         </m:MidocoCredentials>
    //     </SOAP-ENV:Header>
    //     <SOAP-ENV:Body>
    //         <SaveCustomerRequest>
    //         </SaveCustomerRequest>
    //     </SOAP-ENV:Body>
    // </SOAP-ENV:Envelope>

    CkXmlW destXml;
    destXml.put_Tag(L"SOAP-ENV:Envelope");
    destXml.AddAttribute(L"xmlns:SOAP-ENV",L"http://schemas.xmlsoap.org/soap/envelope/");
    destXml.AddAttribute(L"xmlns:SOAP-ENC",L"http://schemas.xmlsoap.org/soap/encoding/");
    destXml.AddAttribute(L"xmlns:xsi",L"http://www.w3.org/2001/XMLSchema-instance");
    destXml.AddAttribute(L"xmlns:xsd",L"http://www.w3.org/2001/XMLSchema");
    destXml.UpdateAttrAt(L"SOAP-ENV:Header|m:Credentials",true,L"xmlns:m",L"http://www.midoco.de/system");
    destXml.UpdateChildContent(L"SOAP-ENV:Header|m:Credentials|m:Login",L"User");
    destXml.UpdateChildContent(L"SOAP-ENV:Header|m:Credentials|m:Password",L"Pass");
    destXml.UpdateChildContent(L"SOAP-ENV:Header|m:Credentials|m:OrgUnit",L"ABC");
    destXml.UpdateChildContent(L"SOAP-ENV:Header|m:Credentials|m:Locale",L"de_DE");
    destXml.UpdateChildContent(L"SOAP-ENV:Body|SaveCustomerRequest",L"");

    // We want to move the "CrmCustomer" subtree in the source XML to inside the "SaveCustomerRequest" element in the destination.

    // Navigate to CrmCustomer
    CkXmlW *crmCust = srcXml.FindChild(L"soapenv:Body|GetCustomerResponse|CrmCustomer");
    if (srcXml.get_LastMethodSuccess() != true) {
        wprintf(L"Failed to find CrmCustomer element.\n");
        return;
    }

    // Navigate to SaveCustomerRequest
    CkXmlW *crmSaveCust = destXml.FindChild(L"SOAP-ENV:Body|SaveCustomerRequest");
    if (destXml.get_LastMethodSuccess() != true) {
        wprintf(L"Failed to find SaveCustomerRequest element.\n");
        return;
    }

    // Move CrmCustomer tree to SaveCustomerRequest.
    crmSaveCust->InsertChildTreeBefore(0,*crmCust);

    delete crmCust;
    delete crmSaveCust;

    // Look at the resulting destXml.  You can see the CrmCustomer subtree moved to underneath SaveCustomerRequest.
    wprintf(L"%s\n",destXml.getXml());

    // <?xml version="1.0" encoding="utf-8"?>
    // <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    //     <SOAP-ENV:Header>
    //         <m:Credentials xmlns:m="http://www.midoco.de/system">
    //             <m:Login>User</m:Login>
    //             <m:Password>Pass</m:Password>
    //             <m:OrgUnit>ABC</m:OrgUnit>
    //             <m:Locale>de_DE</m:Locale>
    //         </m:Credentials>
    //     </SOAP-ENV:Header>
    //     <SOAP-ENV:Body>
    //         <SaveCustomerRequest>
    //             <CrmCustomer addresseeLine1="Max Mustermann" addresseeLine2="" changingUser="123456">
    //                 <CrmAddress addressId="2225355" addressTypeId="1" checkStatus="O" city="Wien" countryCode="AT" customerId="000071"/>
    //                 <CrmPerson birthDay="30" birthMonth="8" birthYear="1977" birthday="1977-08-30T00:00:00.000+02:00" birthdayNotProvided="false"/>
    //             </CrmCustomer>
    //         </SaveCustomerRequest>
    //     </SOAP-ENV:Body>
    // </SOAP-ENV:Envelope>

    // Look at the resulting srcXml.  The CrmCustomer subtree was removed.
    wprintf(L"%s\n",srcXml.getXml());

    // <?xml version="1.0" encoding="utf-8"?>
    // <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    //     <soapenv:Header/>
    //     <soapenv:Body>
    //         <GetCustomerResponse xmlns="http://www.midoco.de/crm" xmlns:tns="http://www.midoco.de/ws"/>
    //     </soapenv:Body>
    // </soapenv:Envelope>
    }