![]()  | 
  
Chilkat  HOME  Android™  AutoIt  C  C#  C++  Chilkat2-Python  CkPython  Classic ASP  DataFlex  Delphi DLL  Go  Java  Node.js  Objective-C  PHP Extension  Perl  PowerBuilder  PowerShell  PureBasic  Ruby  SQL Server  Swift  Tcl  Unicode C  Unicode C++  VB.NET  VBScript  Visual Basic 6.0  Visual FoxPro  Xojo Plugin
 
      (Tcl) Find and Update an XML Attribute ValueSee more XML ExamplesDemonstrates how to find an element in XML with a specified attribute name and then update the attribute value.
 load ./chilkat.dll # Let's say you have 2 XML files with identical structures as shown directly below, # and you wish to update the values in the 1st XML file with those in the 2nd XML file. # <global> # <phrase id="2FADisabled" value="2factor authentication disabled"/> # <phrase id="2FAEnabled" value="2factor authentication enabled"/> # <phrase id="2FAResetFailed" value="Failed to disable two factor authentication"/> # </global> # <global> # <phrase id="2FAResetFailed" value="Failed to reset two factor authentication"/> # <phrase id="2FADisabled" value="2FA authentication disabled"/> # <phrase id="2FAEnabled" value="2FA authentication enabled"/> # </global> # First, initialize each of the Chilkat XML objects. # These could be loaded from files, but we'll just create them manually for this example.. set xml1 [new_CkXml] CkXml_put_Tag $xml1 "global" CkXml_UpdateAttrAt $xml1 "phrase" 1 "id" "2FADisabled" CkXml_UpdateAttrAt $xml1 "phrase" 1 "value" "2factor authentication disabled" CkXml_UpdateAttrAt $xml1 "phrase[1]" 1 "id" "2FAEnabled" CkXml_UpdateAttrAt $xml1 "phrase[1]" 1 "value" "2factor authentication enabled" CkXml_UpdateAttrAt $xml1 "phrase[2]" 1 "id" "2FAResetFailed" CkXml_UpdateAttrAt $xml1 "phrase[2]" 1 "value" "Failed to disable two factor authentication" set xml2 [new_CkXml] CkXml_put_Tag $xml2 "global" CkXml_UpdateAttrAt $xml2 "phrase" 1 "id" "2FAResetFailed" CkXml_UpdateAttrAt $xml2 "phrase" 1 "value" "Failed to reset two factor authentication" CkXml_UpdateAttrAt $xml2 "phrase[1]" 1 "id" "2FADisabled" CkXml_UpdateAttrAt $xml2 "phrase[1]" 1 "value" "2FA authentication disabled" CkXml_UpdateAttrAt $xml2 "phrase[2]" 1 "id" "2FAEnabled" CkXml_UpdateAttrAt $xml2 "phrase[2]" 1 "value" "2FA authentication enabled" # Iterate over attribute values in xml2 and update the same in xml1. set sbPath [new_CkStringBuilder] set i 0 set count_i [CkXml_NumChildrenHavingTag $xml2 "phrase"] while {$i < $count_i} { CkXml_put_I $xml2 $i set phrase_id [CkXml_chilkatPath $xml2 "phrase[i]|(id)"] set phrase_value [CkXml_chilkatPath $xml2 "phrase[i]|(value)"] # Notice how the UpdateAttrAt method's 1st argument is a Chilkat path -- which is an expression # to find the location of the XML element to be updated. # Here we create a path of the form "/A/tagName,attributeName,attributeValuePattern", which means # find the direct descendent of xml2 having tag=tagName, with an attribute=attributeName where the attribute value # matches the attributeValuePattern. If attributeValuePattern does not contain a "*", then it must match directly. # We'll build a Chilkat path such as "/A/phrase,id,2FADisabled" CkStringBuilder_SetString $sbPath "/A/phrase,id," CkStringBuilder_Append $sbPath $phrase_id # This assumes the corresponding element exists in xml1. CkXml_UpdateAttrAt $xml1 [CkStringBuilder_getAsString $sbPath] 1 "value" $phrase_value set i [expr $i + 1] } # Now see how xml1 has been updated.. puts [CkXml_getXml $xml1] # <global> # <phrase id="2FADisabled" value="2FA authentication disabled"/> # <phrase id="2FAEnabled" value="2FA authentication enabled"/> # <phrase id="2FAResetFailed" value="Failed to reset two factor authentication"/> # </global> delete_CkXml $xml1 delete_CkXml $xml2 delete_CkStringBuilder $sbPath  | 
  ||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.