2 Replies Latest reply on Nov 5, 2009 7:19 AM by jsiggia

    updating existing xml doc

    jsiggia

      i have an xml doc that looks like this

       

      <?xml version="1.0" encoding="UTF-8"?>
      <messageList>
            <message>

                <sender>john</sender>

                <forPage>home</forPage>
           </message>

      </messagelist>

       

      i want to add another element called <forPage> under the current one.(one to many relationship). my code below overwrite the first entered <forPage>

       

       

         <cfset arrayInsertAt(xApps.messageList.xmlChildren,1,xmlElemNew(xApps,'message'))>
         <cfset xApps.messageList.message[1].sender = xmlElemNew(xApps,'sender')>
         <cfset xApps.messageList.message[1].sender.xmlText = "john">

         <cfset xApps.messageList.message[1].forPage  = xmlElemNew(xApps,'forPage')>
         <cfset xApps.messageList.message[1].forPage.xmlText = "home">   
         <cfset xApps.messageList.message[1].forPage = xmlElemNew(xApps,'forPage')>
         <cfset xApps.messageList.message[1].forPage.xmlText= "sales">

       

      i am looking to update my xml like this

       

      <?xml version="1.0" encoding="UTF-8"?>
      <messageList>
            <message>

                <sender>john</sender>

                <forPage>home</forPage>

                <forPage>sale</forPage>

           </message>

      </messagelist>

       

      this code will eventually be put into a loop to account for numerous occurrences of <forpage>.

      thanks for your time

        • 1. Re: updating existing xml doc
          ilssac Level 5
             <cfset xApps.messageList.message[1].forPage  = xmlElemNew(xApps,'forPage')>
             <cfset xApps.messageList.message[1].forPage.xmlText = "home">    
             <cfset xApps.messageList.message[1].forPage = xmlElemNew(xApps,'forPage')>
             <cfset xApps.messageList.message[1].forPage.xmlText= "sales">

           

          If you want multiple forPage elements you need to treat it like an array just like you did earlier to add a message element to the xmlChildren array of message list.

           


          Here is a example hardcoded to just add two forPage elements.  You would probably need to incorporate other array functions to modify this for a loop such as arrayLen().

           

          <cfset arrayInsertAt(xApps.messageList.xmlChildren,1,xmlElemNew(xApps,'message'))>
          <cfset xApps.messageList.message[1].sender = xmlElemNew(xApps,'sender')>
          <cfset xApps.messageList.message[1].sender.xmlText = "john">
          
          <cfset arrayAppend(xApps.messageList.message[1].xmlChildren,xmlElemNew(xApps,'forPage'))>
          <cfset xApps.messageList.message[1].forPage[1].xmlText = "home">
          
          <cfset arrayAppend(xApps.messageList.message[1].xmlChildren,xmlElemNew(xApps,'forPage'))>
          <cfset xApps.messageList.message[1].forPage[2].xmlText= "sales">
          
          • 2. Re: updating existing xml doc
            jsiggia Level 1

            you are correct. i was trying it like this

             

            <cfset arrayInsertAt(xApps.messageList.message[2].xmlChildren,2,xmlElemNew(xApps,'forPage'))>

             

            but was getting an error.

             

            changed the code to below and all works fine.  thanks for your time

             

             

            <cfset arrayInsertAt(xApps.messageList.message.xmlChildren,2,xmlElemNew(xApps,'forPage'))>