Tcl
Tcl
Move XML Subtree to Another XML Document
Demonstrates using the InsertChildTreeBefore method to move a fragment of XML from one document to another.Chilkat Tcl Downloads
load ./chilkat.dll
# 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.
set srcXml [new_CkXml]
CkXml_put_Tag $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" 1 "xmlns" "http://www.midoco.de/crm"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse" 1 "xmlns:tns" "http://www.midoco.de/ws"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer" 1 "addresseeLine1" "Max Mustermann"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer" 1 "addresseeLine2" ""
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer" 1 "changingUser" "123456"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress" 1 "addressId" "2225355"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress" 1 "addressTypeId" "1"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress" 1 "checkStatus" "O"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress" 1 "city" "Wien"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress" 1 "countryCode" "AT"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer|CrmAddress" 1 "customerId" "000071"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson" 1 "birthDay" "30"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson" 1 "birthMonth" "8"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson" 1 "birthYear" "1977"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson" 1 "birthday" "1977-08-30T00:00:00.000+02:00"
CkXml_UpdateAttrAt $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer|CrmPerson" 1 "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>
set destXml [new_CkXml]
CkXml_put_Tag $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" 1 "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 is a CkXml
set crmCust [CkXml_FindChild $srcXml "soapenv:Body|GetCustomerResponse|CrmCustomer"]
if {[CkXml_get_LastMethodSuccess $srcXml] != 1} then {
puts "Failed to find CrmCustomer element."
delete_CkXml $srcXml
delete_CkXml $destXml
exit
}
# Navigate to SaveCustomerRequest
# crmSaveCust is a CkXml
set crmSaveCust [CkXml_FindChild $destXml "SOAP-ENV:Body|SaveCustomerRequest"]
if {[CkXml_get_LastMethodSuccess $destXml] != 1} then {
puts "Failed to find SaveCustomerRequest element."
delete_CkXml $srcXml
delete_CkXml $destXml
exit
}
# Move CrmCustomer tree to SaveCustomerRequest.
CkXml_InsertChildTreeBefore $crmSaveCust 0 $crmCust
delete_CkXml $crmCust
delete_CkXml $crmSaveCust
# Look at the resulting destXml. You can see the CrmCustomer subtree moved to underneath SaveCustomerRequest.
puts [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.
puts [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>
delete_CkXml $srcXml
delete_CkXml $destXml