PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
oleobject loo_Xml1
oleobject loo_Xml2
string ls_Phrase_id
string ls_Phrase_value
oleobject loo_SbPath
integer i
integer li_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..
loo_Xml1 = create oleobject
li_rc = loo_Xml1.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
destroy loo_Xml1
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Xml1.Tag = "global"
loo_Xml1.UpdateAttrAt("phrase",1,"id","2FADisabled")
loo_Xml1.UpdateAttrAt("phrase",1,"value","2factor authentication disabled")
loo_Xml1.UpdateAttrAt("phrase[1]",1,"id","2FAEnabled")
loo_Xml1.UpdateAttrAt("phrase[1]",1,"value","2factor authentication enabled")
loo_Xml1.UpdateAttrAt("phrase[2]",1,"id","2FAResetFailed")
loo_Xml1.UpdateAttrAt("phrase[2]",1,"value","Failed to disable two factor authentication")
loo_Xml2 = create oleobject
li_rc = loo_Xml2.ConnectToNewObject("Chilkat.Xml")
loo_Xml2.Tag = "global"
loo_Xml2.UpdateAttrAt("phrase",1,"id","2FAResetFailed")
loo_Xml2.UpdateAttrAt("phrase",1,"value","Failed to reset two factor authentication")
loo_Xml2.UpdateAttrAt("phrase[1]",1,"id","2FADisabled")
loo_Xml2.UpdateAttrAt("phrase[1]",1,"value","2FA authentication disabled")
loo_Xml2.UpdateAttrAt("phrase[2]",1,"id","2FAEnabled")
loo_Xml2.UpdateAttrAt("phrase[2]",1,"value","2FA authentication enabled")
// Iterate over attribute values in xml2 and update the same in xml1.
loo_SbPath = create oleobject
li_rc = loo_SbPath.ConnectToNewObject("Chilkat.StringBuilder")
i = 0
li_Count_i = loo_Xml2.NumChildrenHavingTag("phrase")
do while i < li_Count_i
loo_Xml2.I = i
ls_Phrase_id = loo_Xml2.ChilkatPath("phrase[i]|(id)")
ls_Phrase_value = loo_Xml2.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"
loo_SbPath.SetString("/A/phrase,id,")
loo_SbPath.Append(ls_Phrase_id)
// This assumes the corresponding element exists in xml1.
loo_Xml1.UpdateAttrAt(loo_SbPath.GetAsString(),1,"value",ls_Phrase_value)
i = i + 1
loop
// Now see how xml1 has been updated..
Write-Debug loo_Xml1.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>
destroy loo_Xml1
destroy loo_Xml2
destroy loo_SbPath