Replace XML values in XML Object

Using CF to find and replace xml values in an XML Object

Page 1 of 1

0 Replies - 2104 Views - Last Post: 28 May 2009 - 05:25 AM Rate Topic: -----

#1 midasxl   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 215
  • Joined: 03-December 08

Replace XML values in XML Object

Posted 28 May 2009 - 05:25 AM

Hello and thanks for your time. I have a bit of code that successfully parses an xml string to a XML Object and then basically traverses it in a number of ways to get to the information. Attached you will find a dump of the XML object in question.

As you can see in the code I am able to search the object for name/value pairs, and also create an array and delete a child at a specific position. What I would like to do is target a specific child and replace its value with another one. Not sure how to go about this. Any suggestions will be greatly appreciated, thanks!!

<cfset myRequest=XMLParse(trim(#inxml#), false)>
<h3>Dump of request XML object<h3/>
<cfdump var="#myRequest#"><br />

<!--- <cfset XMLText=ToString(#myRequest#)>
<cfdump var="#XMLText#"><br />
 --->
<!--- <cfset foo = XmlSearch( XML_DOCUMENT, XPATH_QUERY ) /> Always returns an array of nodes.  If the XPath argument does not result in any matching nodes, XmlSearch() will return an empty array. So long as your XPath syntax is valid, XmlSearch() will never throw an error.
--->
<cfset searchNodes = XmlSearch(#myRequest#, "//*[ name() = 'uic' ]") />
 

<!--- Dump out results. --->
<cfdump
var="#searchNodes#"
label="All Nodes that have XmlName = uic"
/><br />

<cfset searchNodes = XmlSearch(#myRequest#, "//*[ text() = 'N00018' ]") />
 

<!--- Dump out results. --->
<cfdump
var="#searchNodes#"
label="All Nodes that have XmlText = N00018"
/><br />

<cfdump var="#myRequest#">

<cfset arrUic=myRequest.Envelope.Body.XmlChildren>

<cfoutput>
<p>Here is a loop of the above xml document object, extracting the xml children of envelope.body</p>
 <ol>
 <cfloop index="i" from="1" to="#ArrayLen(arrUic)#">
  <li>#arrUic[i]#</li>
 </cfloop>
 </ol>
 
 <cfset notAUic = XmlChildPos(myRequest.Envelope.Body,"getSubordinates",1)>
 
 <div style="color:red;">
  The UIC at position #notAUic# is not a valid UIC and has been deleted.
 </div>
 
 <cfset ArrayDeleteAt(arrUic,notAUic)>
 
 <ol>
 <cfloop index="i" from="1" to="#ArrayLen(arrUic)#">
  <li>#arrUic[i]#</li>
 </cfloop>
 </ol>
 	<p>No more UIC's</p>
</cfoutput>

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Page 1 of 1