0
Wrting to an XML file using Coldfusion

/t5/coldfusion-discussions/wrting-to-an-xml-file-using-coldfusion/td-p/866304
Dec 18, 2008
Dec 18, 2008
Copy link to clipboard
Copied
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.
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/wrting-to-an-xml-file-using-coldfusion/m-p/866305#M79904
Dec 19, 2008
Dec 19, 2008
Copy link to clipboard
Copied
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.
> 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.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Guest
AUTHOR
/t5/coldfusion-discussions/wrting-to-an-xml-file-using-coldfusion/m-p/866306#M79905
Dec 19, 2008
Dec 19, 2008
Copy link to clipboard
Copied
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.
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.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
LATEST
/t5/coldfusion-discussions/wrting-to-an-xml-file-using-coldfusion/m-p/866310#M79909
Dec 21, 2008
Dec 21, 2008
Copy link to clipboard
Copied
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.
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.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/wrting-to-an-xml-file-using-coldfusion/m-p/866307#M79906
Dec 19, 2008
Dec 19, 2008
Copy link to clipboard
Copied
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.
> 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.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Guest
AUTHOR
/t5/coldfusion-discussions/wrting-to-an-xml-file-using-coldfusion/m-p/866308#M79907
Dec 19, 2008
Dec 19, 2008
Copy link to clipboard
Copied
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>
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>
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Guest
AUTHOR
/t5/coldfusion-discussions/wrting-to-an-xml-file-using-coldfusion/m-p/866309#M79908
Dec 19, 2008
Dec 19, 2008
Copy link to clipboard
Copied
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>
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>
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

