Delphi DLL
Delphi DLL
Move XML Subtree to Another XML Document
Demonstrates using the InsertChildTreeBefore method to move a fragment of XML from one document to another.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Xml;
...
procedure TForm1.Button1Click(Sender: TObject);
var
srcXml: HCkXml;
destXml: HCkXml;
crmCust: HCkXml;
crmSaveCust: HCkXml;
begin
// 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) then
begin
Memo1.Lines.Add('Failed to find CrmCustomer element.');
Exit;
end;
// Navigate to SaveCustomerRequest
crmSaveCust := CkXml_FindChild(destXml,'SOAP-ENV:Body|SaveCustomerRequest');
if (CkXml_getLastMethodSuccess(destXml) <> True) then
begin
Memo1.Lines.Add('Failed to find SaveCustomerRequest element.');
Exit;
end;
// 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.
Memo1.Lines.Add(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.
Memo1.Lines.Add(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);
end;