Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Delphi DLL Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(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 for Delphi Downloads

Chilkat non-ActiveX DLL for Delphi

Chilkat ActiveX DLL for Delphi

* The examples here use the non-ActiveX DLL.

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;

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.