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

DataFlex 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
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

 

 

 

(DataFlex) Find and Update an XML Attribute Value

See more XML Examples

Demonstrates how to find an element in XML with a specified attribute name and then update the attribute value.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoXml1
    Boolean iSuccess
    Handle hoXml2
    String sPhrase_id
    String sPhrase_value
    Handle hoSbPath
    Integer i
    Integer iCount_i
    String sTemp1

    // 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..

    Get Create (RefClass(cComChilkatXml)) To hoXml1
    If (Not(IsComObjectCreated(hoXml1))) Begin
        Send CreateComObject of hoXml1
    End
    Set ComTag Of hoXml1 To "global"
    Get ComUpdateAttrAt Of hoXml1 "phrase" True "id" "2FADisabled" To iSuccess
    Get ComUpdateAttrAt Of hoXml1 "phrase" True "value" "2factor authentication disabled" To iSuccess
    Get ComUpdateAttrAt Of hoXml1 "phrase[1]" True "id" "2FAEnabled" To iSuccess
    Get ComUpdateAttrAt Of hoXml1 "phrase[1]" True "value" "2factor authentication enabled" To iSuccess
    Get ComUpdateAttrAt Of hoXml1 "phrase[2]" True "id" "2FAResetFailed" To iSuccess
    Get ComUpdateAttrAt Of hoXml1 "phrase[2]" True "value" "Failed to disable two factor authentication" To iSuccess

    Get Create (RefClass(cComChilkatXml)) To hoXml2
    If (Not(IsComObjectCreated(hoXml2))) Begin
        Send CreateComObject of hoXml2
    End
    Set ComTag Of hoXml2 To "global"
    Get ComUpdateAttrAt Of hoXml2 "phrase" True "id" "2FAResetFailed" To iSuccess
    Get ComUpdateAttrAt Of hoXml2 "phrase" True "value" "Failed to reset two factor authentication" To iSuccess
    Get ComUpdateAttrAt Of hoXml2 "phrase[1]" True "id" "2FADisabled" To iSuccess
    Get ComUpdateAttrAt Of hoXml2 "phrase[1]" True "value" "2FA authentication disabled" To iSuccess
    Get ComUpdateAttrAt Of hoXml2 "phrase[2]" True "id" "2FAEnabled" To iSuccess
    Get ComUpdateAttrAt Of hoXml2 "phrase[2]" True "value" "2FA authentication enabled" To iSuccess

    // Iterate over attribute values in xml2 and update the same in xml1.

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPath
    If (Not(IsComObjectCreated(hoSbPath))) Begin
        Send CreateComObject of hoSbPath
    End

    Move 0 To i
    Get ComNumChildrenHavingTag Of hoXml2 "phrase" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoXml2 To i
        Get ComChilkatPath Of hoXml2 "phrase[i]|(id)" To sPhrase_id
        Get ComChilkatPath Of hoXml2 "phrase[i]|(value)" To sPhrase_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"
        Get ComSetString Of hoSbPath "/A/phrase,id," To iSuccess
        Get ComAppend Of hoSbPath sPhrase_id To iSuccess

        // This assumes the corresponding element exists in xml1.
        Get ComGetAsString Of hoSbPath To sTemp1
        Get ComUpdateAttrAt Of hoXml1 sTemp1 True "value" sPhrase_value To iSuccess

        Move (i + 1) To i
    Loop

    // Now see how xml1 has been updated..
    Get ComGetXml Of hoXml1 To sTemp1
    Showln sTemp1


End_Procedure

 

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