Skip to main content
Known Participant
October 27, 2009
Question

modify an existing XML file

  • October 27, 2009
  • 1 reply
  • 970 views
i am trying to modify and existing xml file. the code is below.
i am using ArrayInsertAt. the order does not matter just want to add new element under root element. the code below is placing "message" into XML file as <message/>

after insert it looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<messageList>
<message/><message>
<sender>llkkkk</sender>
<creator>adasdasdasd</creator>
<dateCreated>10/10/2008</dateCreated>
</message>
</messageList>

thank you for your time
<cfhttp url="messages_dummy.xml">

<cfset xApps = XmlParse(cfhttp.FileContent)>
<cfset ArrayInsertAt(xApps.messagelist.XmlChildren, 1, XmlElemNew (xApps, "message"))>

<cffile action="write" file="/webdocs/intranet/cfapps/userMessageDev/userMessage/messages_dummy.xml"
output=#toString(xApps)#>
This topic has been closed for replies.

1 reply

ilssac
Inspiring
October 27, 2009

While most of the array functions and notation works fine with XML objects, ArrayInsertAt() is not one of them.

You probably want the xmlElemNew() function, check out the other XML functions. http://livedocs.adobe.com/coldfusion/8/htmldocs/functions-pt0_21.html#3468770

jsiggiaAuthor
Known Participant
October 27, 2009

thanks for the response but it does not seem to change anything. i modified my code to

<cfset xApps.xmlRoot.XmlChildren[1] = XmlElemNew(xApps,"message") />

thinking this would place messages into the first child and renumber the exsiting children. it did not and instead placed messages first under messageList as this:

<messageList>
      <message/>

i am looking to get to this point where i insert a new message before or after an existing one

<messageList>
      <message>

     </message>

     <message>
            <sender>JONES</sender>
      </message>

</messageList>

thanks for your time

ilssac
Inspiring
October 27, 2009

Ok, so it takes a combination of arrayInsertAt, which I now know does work on xml objects, and xmlElemNew.

<cfsavecontent variable="xml">
<messageList>
      <message>
                <sender>SMITH</sender>
     </message>

     <message>
            <sender>JONES</sender>
      </message>

</messageList>
</cfsavecontent>

<cfset xmlObj = xmlParse(xml)>
<cfdump var="#xmlObj#">
<cfset arrayInsertAt(xmlObj.messageList.xmlChildren,1,xmlElemNew(xmlObj,'message'))>
<cfdump var="#xmlObj#">