C
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 C Downloads
#include <C_CkXml.h>
void ChilkatSample(void)
{
HCkXml srcXml;
HCkXml destXml;
HCkXml crmCust;
HCkXml crmSaveCust;
// 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.
srcXml = CkXml_Create();
CkXml_putTag(srcXml,"soapenv:Envelope");
CkXml_AddAttribute(srcXml,"xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/");
CkXml_UpdateChildContent(srcXml,"soapenv:Header","");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse",TRUE,"xmlns","http://www.midoco.de/crm");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse",TRUE,"xmlns:tns","http://www.midoco.de/ws");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer",TRUE,"addresseeLine1","Max Mustermann");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer",TRUE,"addresseeLine2","");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer",TRUE,"changingUser","123456");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress",TRUE,"addressId","2225355");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress",TRUE,"addressTypeId","1");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress",TRUE,"checkStatus","O");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress",TRUE,"city","Wien");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress",TRUE,"countryCode","AT");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress",TRUE,"customerId","000071");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson",TRUE,"birthDay","30");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson",TRUE,"birthMonth","8");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson",TRUE,"birthYear","1977");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson",TRUE,"birthday","1977-08-30T00:00:00.000+02:00");
CkXml_UpdateAttrAt(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson",TRUE,"birthdayNotProvided","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>
destXml = CkXml_Create();
CkXml_putTag(destXml,"SOAP-ENV:Envelope");
CkXml_AddAttribute(destXml,"xmlns:SOAP-ENV","http://schemas.xmlsoap.org/soap/envelope/");
CkXml_AddAttribute(destXml,"xmlns:SOAP-ENC","http://schemas.xmlsoap.org/soap/encoding/");
CkXml_AddAttribute(destXml,"xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
CkXml_AddAttribute(destXml,"xmlns:xsd","http://www.w3.org/2001/XMLSchema");
CkXml_UpdateAttrAt(destXml,"SOAP-ENV:Header|m:Credentials",TRUE,"xmlns:m","http://www.midoco.de/system");
CkXml_UpdateChildContent(destXml,"SOAP-ENV:Header|m:Credentials|m:Login","User");
CkXml_UpdateChildContent(destXml,"SOAP-ENV:Header|m:Credentials|m:Password","Pass");
CkXml_UpdateChildContent(destXml,"SOAP-ENV:Header|m:Credentials|m:OrgUnit","ABC");
CkXml_UpdateChildContent(destXml,"SOAP-ENV:Header|m:Credentials|m:Locale","de_DE");
CkXml_UpdateChildContent(destXml,"SOAP-ENV:Body|SaveCustomerRequest","");
// We want to move the "CrmCustomer" subtree in the source XML to inside the "SaveCustomerRequest" element in the destination.
// Navigate to CrmCustomer
crmCust = CkXml_FindChild(srcXml,"soapenv:Body|GetCustomerResponse|CrmCustomer");
if (CkXml_getLastMethodSuccess(srcXml) != TRUE) {
printf("Failed to find CrmCustomer element.\n");
CkXml_Dispose(srcXml);
CkXml_Dispose(destXml);
return;
}
// Navigate to SaveCustomerRequest
crmSaveCust = CkXml_FindChild(destXml,"SOAP-ENV:Body|SaveCustomerRequest");
if (CkXml_getLastMethodSuccess(destXml) != TRUE) {
printf("Failed to find SaveCustomerRequest element.\n");
CkXml_Dispose(srcXml);
CkXml_Dispose(destXml);
return;
}
// Move CrmCustomer tree to SaveCustomerRequest.
CkXml_InsertChildTreeBefore(crmSaveCust,0,crmCust);
CkXml_Dispose(crmCust);
CkXml_Dispose(crmSaveCust);
// Look at the resulting destXml. You can see the CrmCustomer subtree moved to underneath SaveCustomerRequest.
printf("%s\n",CkXml_getXml(destXml));
// <?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.
printf("%s\n",CkXml_getXml(srcXml));
// <?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>
CkXml_Dispose(srcXml);
CkXml_Dispose(destXml);
}