PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkXml.pb"
Procedure ChilkatExample()
; 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.i = CkXml::ckCreate()
If xml1.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkXml::setCkTag(xml1, "global")
CkXml::ckUpdateAttrAt(xml1,"phrase",1,"id","2FADisabled")
CkXml::ckUpdateAttrAt(xml1,"phrase",1,"value","2factor authentication disabled")
CkXml::ckUpdateAttrAt(xml1,"phrase[1]",1,"id","2FAEnabled")
CkXml::ckUpdateAttrAt(xml1,"phrase[1]",1,"value","2factor authentication enabled")
CkXml::ckUpdateAttrAt(xml1,"phrase[2]",1,"id","2FAResetFailed")
CkXml::ckUpdateAttrAt(xml1,"phrase[2]",1,"value","Failed to disable two factor authentication")
xml2.i = CkXml::ckCreate()
If xml2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkXml::setCkTag(xml2, "global")
CkXml::ckUpdateAttrAt(xml2,"phrase",1,"id","2FAResetFailed")
CkXml::ckUpdateAttrAt(xml2,"phrase",1,"value","Failed to reset two factor authentication")
CkXml::ckUpdateAttrAt(xml2,"phrase[1]",1,"id","2FADisabled")
CkXml::ckUpdateAttrAt(xml2,"phrase[1]",1,"value","2FA authentication disabled")
CkXml::ckUpdateAttrAt(xml2,"phrase[2]",1,"id","2FAEnabled")
CkXml::ckUpdateAttrAt(xml2,"phrase[2]",1,"value","2FA authentication enabled")
; Iterate over attribute values in xml2 and update the same in xml1.
phrase_id.s
phrase_value.s
sbPath.i = CkStringBuilder::ckCreate()
If sbPath.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
i.i = 0
count_i.i = CkXml::ckNumChildrenHavingTag(xml2,"phrase")
While i < count_i
CkXml::setCkI(xml2, i)
phrase_id = CkXml::ckChilkatPath(xml2,"phrase[i]|(id)")
phrase_value = CkXml::ckChilkatPath(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::ckSetString(sbPath,"/A/phrase,id,")
CkStringBuilder::ckAppend(sbPath,phrase_id)
; This assumes the corresponding element exists in xml1.
CkXml::ckUpdateAttrAt(xml1,CkStringBuilder::ckGetAsString(sbPath),1,"value",phrase_value)
i = i + 1
Wend
; Now see how xml1 has been updated..
Debug CkXml::ckGetXml(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>
CkXml::ckDispose(xml1)
CkXml::ckDispose(xml2)
CkStringBuilder::ckDispose(sbPath)
ProcedureReturn
EndProcedure