School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,441 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,522 people online right now. Registration is fast and FREE... Join Now!




Replace XML values in XML Object

 

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

midasxl

28 May, 2009 - 04:25 AM
Post #1

D.I.C Head
**

Joined: 3 Dec, 2008
Posts: 125



Thanked: 1 times
My Contributions
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!!

CODE

<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 thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:23AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month