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

Wrting to an XML file using Coldfusion

Guest
Dec 18, 2008 Dec 18, 2008
Hi All,I need some help with .

I have an XML file like:

<Emp>
<Name></Name>
<City></City>
<State></State>
</Emp>

And one form , where I am trying to submit the form values which are as input fields and add or append the old xml content with the new content.So that the above file looks like:

<Emp>
<Name>Jack</Name>
<City>Jonney</City>
<State>New Jersey</State>
</Emp>
I have seen CFFIle but then it seems to be saving the whole XML file and here I want to just update specific elements.
Thanks in advance.
TOPICS
Getting started
633
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
LEGEND ,
Dec 19, 2008 Dec 19, 2008
jjonney wrote:
> I have seen CFFIle but then it seems to be saving the whole XML file and here
> I want to just update specific elements.
> Thanks in advance.
>

Thats the way it works. The usual process is to load the XML document,
use ColdFusion XML and|or string functions to modify the XML then write
out the file. This does present limits with very large XML files.

If you need something more refined, you need to dive into the Java
underpinnings that do allow for more granular processing of file data.
I can't provide any examples, but I'm sure Google can.
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
Guest
Dec 19, 2008 Dec 19, 2008
Hi .
Thanks for answering .It helped as I was trying to get it work and it just didn't work .
But there is something which is not right in the following code.
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
Community Expert ,
Dec 21, 2008 Dec 21, 2008
LATEST
You can do it in 4 lines, as shown below. Also watch out for the following:

1) Your XML has an error. The attribute must be within quotes, thus: <blog name="First">.
2) Just read the file as text and write it back as text. Don't add the function XMLParse. It could transform <itunessubtitle></itunessubtitle> into <itunessubtitle/> and append <?xml version="1.0" encoding="UTF-8"?>, resulting in a different document.




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
LEGEND ,
Dec 19, 2008 Dec 19, 2008
jjonney wrote:
> But there is something which is not right in the following code.

It would be most helpful if you could provide some idea of what you
expect it to do, what it is doing and|or how they deviate from each other.


>
>
> <cfset RunningLogPath = ExpandPath("blog.xml.cfm")>
>
> <cfoutput>
> <cffile action="read" file="#RunningLogPath#" variable="filecontent" />
> </cfoutput>
> <cfset mydoc = XmlParse(filecontent) />
>
>
> <cfoutput>
> <cfset blogText=mydoc.blogs.blog[3].itunessubtitle.XmlText>
> <cfset blogText="Nice Music">
> <cffile action="write" file="blog.xml.cfm" output="#blogText#">
>
> </cfoutput>

One thing I will say is that you have unnecessary <cfoutput...> tags.
Those are only required when you are, ya know, generating output.
Nearly all ColdFusion functions and tags render variables in an expected
manner.
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
Guest
Dec 19, 2008 Dec 19, 2008
I am trying to add data to the following xml file .So that <itunessubtitle> has a value of Nice Music,which would result to <itunessubtitle> Nice Music </itunessubtitle>

blog.xml.cfm
<blogs>
<blog name=First>
<itunessubtitle></itunessubtitle>
<itunessummary></itunessummary>
<ituneskeywords></ituneskeywords>
<itunesauthor></itunesauthor>
<itunesimage></itunesimage>
<itunesexplicit></itunesexplicit>
</blog>
</blogs>

_____________________

CFM file
<cfset RunningLogPath = ExpandPath("blog.xml.cfm")>

<cfoutput>
<cffile action="read" file="#RunningLogPath#" variable="filecontent" />
</cfoutput>
<cfset mydoc = XmlParse(filecontent) />


<cfoutput>
<cfset blogText=mydoc.blogs.blog[3].itunessubtitle.XmlText>

<cffile action="write" file="blog.xml.cfm" output="#blogText#">

</cfoutput>
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
Guest
Dec 19, 2008 Dec 19, 2008
I am trying to add data to the following xml file .So that <itunessubtitle> has a value of Nice Music,which would result to <itunessubtitle> Nice Music </itunessubtitle>

blog.xml.cfm
<blogs>
<blog name=First>
<itunessubtitle></itunessubtitle>
<itunessummary></itunessummary>
<ituneskeywords></ituneskeywords>
<itunesauthor></itunesauthor>
<itunesimage></itunesimage>
<itunesexplicit></itunesexplicit>
</blog>
</blogs>

_____________________

CFM file
<cfset RunningLogPath = ExpandPath("blog.xml.cfm")>

<cfoutput>
<cffile action="read" file="#RunningLogPath#" variable="filecontent" />
</cfoutput>
<cfset mydoc = XmlParse(filecontent) />


<cfoutput>
<cfset blogText=mydoc.blogs.blog[3].itunessubtitle.XmlText>
<cfset blogText="Nice Music">
<cffile action="write" file="blog.xml.cfm" output="#blogText#">

</cfoutput>
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