Visual FoxPro
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL loXml1
LOCAL loXml2
LOCAL lcPhrase_id
LOCAL lcPhrase_value
LOCAL loSbPath
LOCAL i
LOCAL lnCount_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..
loXml1 = CreateObject('Chilkat.Xml')
loXml1.Tag = "global"
loXml1.UpdateAttrAt("phrase",1,"id","2FADisabled")
loXml1.UpdateAttrAt("phrase",1,"value","2factor authentication disabled")
loXml1.UpdateAttrAt("phrase[1]",1,"id","2FAEnabled")
loXml1.UpdateAttrAt("phrase[1]",1,"value","2factor authentication enabled")
loXml1.UpdateAttrAt("phrase[2]",1,"id","2FAResetFailed")
loXml1.UpdateAttrAt("phrase[2]",1,"value","Failed to disable two factor authentication")
loXml2 = CreateObject('Chilkat.Xml')
loXml2.Tag = "global"
loXml2.UpdateAttrAt("phrase",1,"id","2FAResetFailed")
loXml2.UpdateAttrAt("phrase",1,"value","Failed to reset two factor authentication")
loXml2.UpdateAttrAt("phrase[1]",1,"id","2FADisabled")
loXml2.UpdateAttrAt("phrase[1]",1,"value","2FA authentication disabled")
loXml2.UpdateAttrAt("phrase[2]",1,"id","2FAEnabled")
loXml2.UpdateAttrAt("phrase[2]",1,"value","2FA authentication enabled")
* Iterate over attribute values in xml2 and update the same in xml1.
loSbPath = CreateObject('Chilkat.StringBuilder')
i = 0
lnCount_i = loXml2.NumChildrenHavingTag("phrase")
DO WHILE i < lnCount_i
loXml2.I = i
lcPhrase_id = loXml2.ChilkatPath("phrase[i]|(id)")
lcPhrase_value = loXml2.ChilkatPath("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"
loSbPath.SetString("/A/phrase,id,")
loSbPath.Append(lcPhrase_id)
* This assumes the corresponding element exists in xml1.
loXml1.UpdateAttrAt(loSbPath.GetAsString(),1,"value",lcPhrase_value)
i = i + 1
ENDDO
* Now see how xml1 has been updated..
? loXml1.GetXml()
* <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>
RELEASE loXml1
RELEASE loXml2
RELEASE loSbPath