Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

updating existing xml doc

New Here ,
Nov 05, 2009 Nov 05, 2009

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

TOPICS
Advanced techniques
316
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 05, 2009 Nov 05, 2009
   <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">

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 05, 2009 Nov 05, 2009
LATEST

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'))>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources