7 Replies Latest reply on Oct 27, 2009 9:10 AM by Dani Szwarc

    modify an existing XML file

    jsiggia
      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)#>
        • 1. Re: modify an existing XML file
          ilssac Level 5

          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

          • 2. Re: modify an existing XML file
            jsiggia Level 1

            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

            • 3. Re: modify an existing XML file
              ilssac Level 5

              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#">
              
              • 4. Re: modify an existing XML file
                ilssac Level 5

                A slightly more complete example I played with because I was curious.

                 

                <cfdump var="#xmlObj#">
                
                <cfset arrayInsertAt(xmlObj.messageList.xmlChildren,1,xmlElemNew(xmlObj,'message'))>
                <cfset xmlObj.messageList.message[1].sender = xmlElemNew(xmlObj,'sender')>
                <cfset xmlObj.messageList.message[1].sender.xmlText = "PIERRE">
                
                <cfdump var="#xmlObj#">
                
                • 5. Re: modify an existing XML file
                  jsiggia Level 1

                  yes this does work fine. actually all the examples where xml is created from scratch ,work fine. i need to read an existing xml file and add to it.

                  this does not do that nor do any examples found to this point do that. am i wasting my time trying to do this in cf? should i be looking into another technology to update an xml file?

                   

                  thank you for your time.

                  • 6. Re: modify an existing XML file
                    jsiggia Level 1

                    wait.........  that last example you sent has much better results......  it placed the sender in the xml doc with the correct results.   let me fool around with this and get back to you.   i had something similar but without your results.

                     

                    thanks for your time

                    • 7. Re: modify an existing XML file
                      Dani Szwarc

                      Hello, let me add my two cents:

                       

                      I came accross the same situation for a photo gallery I needed my client to change. It is a flash/xml photo gallery and the client wanted to change/add/delete images.

                       

                      My solution (which for sure is not the best, but it works) is:

                       

                      1) Every time the client adds a picture to the gallery, I write the info into the database.

                      2) Then I call a page that runs the cfxml tag and within the tag it runs a query on that table. This query forms the updated xml file and replaces the old one.

                       

                      I hope this was clear, otherwise I can send you the code.

                       

                      Cheers,