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

modify an existing XML file

New Here ,
Oct 27, 2009 Oct 27, 2009

Copy link to clipboard

Copied

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)#>
TOPICS
Advanced techniques

Views

847

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 ,
Oct 27, 2009 Oct 27, 2009

Copy link to clipboard

Copied

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

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 ,
Oct 27, 2009 Oct 27, 2009

Copy link to clipboard

Copied

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

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
Valorous Hero ,
Oct 27, 2009 Oct 27, 2009

Copy link to clipboard

Copied

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

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 ,
Oct 27, 2009 Oct 27, 2009

Copy link to clipboard

Copied

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.

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 ,
Oct 27, 2009 Oct 27, 2009

Copy link to clipboard

Copied

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

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
Participant ,
Oct 27, 2009 Oct 27, 2009

Copy link to clipboard

Copied

LATEST

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,

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
Valorous Hero ,
Oct 27, 2009 Oct 27, 2009

Copy link to clipboard

Copied

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

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