coldfusion - How do I delete an xml node by attribute value? -


i have xml object in coldfusion code looks this:

enter image description here

i have ability delete node based on zip. example - delete node has zip of '22222'.

i know can loop through each node, check zip, , once find execute <cfset arraydeleteat(xmldoc.terminals.xmlchildren, currentindex)> delete node. there more intuitive way this? built in coldfusion functions avoid explicit looping?

i use jsoup this. it's html parser, can still used against xml.

once you've parsed xml, selecting , removing attribute simple as...

.select('[zip=22222]').remove() 

...but here complete example, using jsoup 1.7.2 (earlier versions may have different api):

<cfsavecontent variable="xmltext">     <terminals>         <terminal location="some random location" terminal="25" zip="11111" />         <terminal location="some other location" terminal="26" zip="22222" />         <terminal location="some more location" terminal="22" zip="33333" />     </terminals> </cfsavecontent>  <cfscript>     jsoup = createobject('java','org.jsoup.jsoup');     parser = createobject('java','org.jsoup.parser.parser');      dom = jsoup.parse( xmltext , '' , parser.xmlparser() );      dom.select('[zip=22222]').remove();  </cfscript>  <cfdump var=#dom.outerhtml()# /> 


code work, cf needs know jsoup classes, needs able see jar file, can achieved in 4 different ways:

option 1:
place jsoup jar in cf's lib directory - e.g. {cf10}/cfusion/webroot/web-inf/lib or {cf9}/webroot/web-inf/lib - , restart cf. more info.

option 2:
place jsoup jar in local directory , setup this.javasettings in application.cfc - feature added in cf 10 , railo 4.0

option 3:
place jsoup jar in local directory , use javaloader load (similar above, works version).

the 2 createobject lines above should replaced with:

jl = new javaloader.javaloader([expandpath('./jsoup-1.7.2.jar')]); jsoup  = jl.create('org.jsoup.jsoup'); parser = jl.create('org.jsoup.parser.parser'); 

(assuming jar file in same directory script)

option 4:
railo , openbd, above options work, or can reference path jar file third argument createobject calls.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -