Update xml file with ColdFusion -


i have xml file needs updated. user wants able select year , amount. best way?

thanks

<root> <sga> <year>2008</year> <amt>940</amt> </sga> <sga> <year>2009</year> <amt>980</amt> </sga> <sga> <year>2010</year> <amt>1000</amt> </sga>   </root> 

you want use 'contains' operator (alejandro points out that's not strict match) match in xpath. execute xpath in coldfusion, use xmlsearch function on xml object. normalize-space() function trims leading , trailing whitespace (fixing, instance cr in 2010 year node).

because xpath matching year node directly, use '/..' fetch year node's parent. if wanted operate on of other sibling nodes year (for instance if there "quantity" node or something).

<cfxml variable="foo"> <root> <sga> <year>2008</year> <amt>940</amt> </sga> <sga> <year>2009</year> <amt>980</amt> </sga> <sga> <year>2010 </year> <amt>1000</amt> </sga>   </root> </cfxml>   <cfset targetyear=" 2010"> <cfset newamount=2000>  <cfdump var="#foo#">  <!--- returns array of matching nodes. ---> <cfset bar = xmlsearch(foo,"/root/sga/year[normalize-space()='#trim(targetyear)#']/..")>  <cfdump var="#bar#">  <cfset bar[1].amt.xmltext = newamount>  <cfdump var="#foo#"> 

in real application, you'd want iterate on results of xmlsearch (bar in case) array, there exists possibility 0 or more 1 result.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -