Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) 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.
#include <C_CkXmlW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkXmlW xml1; HCkXmlW xml2; const wchar_t *phrase_id; const wchar_t *phrase_value; HCkStringBuilderW sbPath; int i; int count_i; // 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.. xml1 = CkXmlW_Create(); CkXmlW_putTag(xml1,L"global"); CkXmlW_UpdateAttrAt(xml1,L"phrase",TRUE,L"id",L"2FADisabled"); CkXmlW_UpdateAttrAt(xml1,L"phrase",TRUE,L"value",L"2factor authentication disabled"); CkXmlW_UpdateAttrAt(xml1,L"phrase[1]",TRUE,L"id",L"2FAEnabled"); CkXmlW_UpdateAttrAt(xml1,L"phrase[1]",TRUE,L"value",L"2factor authentication enabled"); CkXmlW_UpdateAttrAt(xml1,L"phrase[2]",TRUE,L"id",L"2FAResetFailed"); CkXmlW_UpdateAttrAt(xml1,L"phrase[2]",TRUE,L"value",L"Failed to disable two factor authentication"); xml2 = CkXmlW_Create(); CkXmlW_putTag(xml2,L"global"); CkXmlW_UpdateAttrAt(xml2,L"phrase",TRUE,L"id",L"2FAResetFailed"); CkXmlW_UpdateAttrAt(xml2,L"phrase",TRUE,L"value",L"Failed to reset two factor authentication"); CkXmlW_UpdateAttrAt(xml2,L"phrase[1]",TRUE,L"id",L"2FADisabled"); CkXmlW_UpdateAttrAt(xml2,L"phrase[1]",TRUE,L"value",L"2FA authentication disabled"); CkXmlW_UpdateAttrAt(xml2,L"phrase[2]",TRUE,L"id",L"2FAEnabled"); CkXmlW_UpdateAttrAt(xml2,L"phrase[2]",TRUE,L"value",L"2FA authentication enabled"); // Iterate over attribute values in xml2 and update the same in xml1. sbPath = CkStringBuilderW_Create(); i = 0; count_i = CkXmlW_NumChildrenHavingTag(xml2,L"phrase"); while (i < count_i) { CkXmlW_putI(xml2,i); phrase_id = CkXmlW_chilkatPath(xml2,L"phrase[i]|(id)"); phrase_value = CkXmlW_chilkatPath(xml2,L"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" CkStringBuilderW_SetString(sbPath,L"/A/phrase,id,"); CkStringBuilderW_Append(sbPath,phrase_id); // This assumes the corresponding element exists in xml1. CkXmlW_UpdateAttrAt(xml1,CkStringBuilderW_getAsString(sbPath),TRUE,L"value",phrase_value); i = i + 1; } // Now see how xml1 has been updated.. wprintf(L"%s\n",CkXmlW_getXml(xml1)); CkXmlW_Dispose(xml1); CkXmlW_Dispose(xml2); CkStringBuilderW_Dispose(sbPath); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.