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

updating existing xml doc

New Here ,
Nov 05, 2009 Nov 05, 2009

Copy link to clipboard

Copied

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

Views

290

Translate

Translate

Report

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

Copy link to clipboard

Copied

   <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">

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Documentation