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

How to remove white spaces from XML content using Coldfusion?

Guest
Dec 08, 2009 Dec 08, 2009

Hi,

Can anybody help me in removing white spaces in between the tags from the below XML content using coldfusion?

XML content:

<?xml version="1.0" encoding="UTF-8"?> <chart showdates="true" today="08/12/2009"> <phases> <phase color="CCFFCC" name="Funded"/> <phase color="CDCD67" name="Concept"/> <phase color="99CCFF" name="Feasibility"/> <phase color="0099FF" name="Development"/> <phase color="0099FF" name="Development"/> <phase color="CC99FF" name="Close-out"/> <phase color="909090" name="Sustaining"/> </phases><program name=""> <project enddate=" 30/03/2007 " id="43250" startdate=" 28/02/2006 "> <version enddate=" 30/03/2007 " number=" 1" startdate=" 28/02/2006 "> <phase color="CCFFCC" currentdate="23/03/2006" name="Project Start" plandate="28/02/2006" type="phase"/> <phase color="99CCFF" currentdate="04/04/2006" name="Feasibility Closure" plandate="31/05/2006" type="phase"/> <phase color="0099FF" currentdate="29/03/2007" name="Commercialization" plandate="30/12/2006" type="phase"/> <phase color="CC99FF" currentdate="30/03/2007" name="Project Closed" plandate="30/03/2007" type="phase"/> <phase color="909090" currentdate="" name="Obsolescence" plandate="" type="phase"/> </version> </project> </program> </chart>


Output I am expecting is like below,

<?xml version="1.0" encoding="UTF-8"?><chart showdates="true" today="08/12/2009"><phases><phase color="CCFFCC" name="Funded"/><phase color="CDCD67" name="Concept"/><phase color="99CCFF" name="Feasibility"/><phase color="0099FF" name="Development"/><phase color="0099FF" name="Development"/><phase color="CC99FF" name="Close-out"/><phase color="909090" name="Sustaining"/></phases><program name=""><project enddate=" 30/03/2007 " id="43250" startdate=" 28/02/2006 "><version enddate=" 30/03/2007 " number=" 1" startdate=" 28/02/2006 "><phase color="CCFFCC" currentdate="23/03/2006" name="Project Start" plandate="28/02/2006" type="phase"/><phase color="99CCFF" currentdate="04/04/2006" name="Feasibility Closure" plandate="31/05/2006" type="phase"/><phase color="0099FF" currentdate="29/03/2007" name="Commercialization" plandate="30/12/2006" type="phase"/><phase color="CC99FF" currentdate="30/03/2007" name="Project Closed" plandate="30/03/2007" type="phase"/><phase color="909090" currentdate="" name="Obsolescence" plandate="" type="phase"/></version> </project></program></chart>


Thanks in advance,


Regards,
Manoz.

2.7K
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
Advocate ,
Dec 08, 2009 Dec 08, 2009

Hi Manoz,

You can use,

<cfscript>
        getPageContext().getOut().clearBuffer();
        writeOutput(your_cfsavecontent_variable);
</cfscript>

for better whitespace management with XML.

You can find more info here,

http://blogs.adobe.com/cantrell/archives/2003/04/controlling_whi.html

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 08, 2009 Dec 08, 2009

Sorry I could not achieve the result, can you please elaborate the solution more.

I have attached my XML file, Please find the attachment.

My requirements are,

1) When I open the XML file using notepad it is showing up lot many whitespaces in the attribute values like below.

<version enddate="                         01/12/2009     " number="                                          1" startdate="                              01/01/2007     ">

I want to remove these extra spaces. the output should be,

<version enddate="01/12/2009" number="1" startdate="01/01/2007">


2) And I need to remove the white spaces in between the tags. Right now my XML in notepad is like below.

<?xml version="1.0" encoding="UTF-8"?> <chart showdates="true" today="08/12/2009"> <phases>

I want to remove the white spaces between the tags, output should be,

<?xml version="1.0" encoding="UTF-8"?><chart showdates="true" today="08/12/2009"><phases>


NOTE:
When I open the XML file using Internet explorer, the XML content is displaying properly.

Over all I want proper XML data(after performing above 2 operations) in a notepad.

Please assist me, Thanks in advance.


Regards,
Manoz.

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
Advocate ,
Dec 08, 2009 Dec 08, 2009

Manoz,

Couple of questions,

1) Are you writing that xml file using CF?.


2) Since you mentioned that it is being displayed properly in IE, what is the need of eliminating those whitespaces?.

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 09, 2009 Dec 09, 2009

Please find my comments below,

1) Yes, I am writing the XML using CF.

   Below is the code(Blue color part):

   The output of the below code in attached in my previous post(attachment name = 49587.xml)

<cfoutput><cfxml variable="MyXMLDoc"><?xml version="1.0" encoding="UTF-8" ?><chart today="#todaysDate#" showdates="true"><phases><cfloop query="qPhases"><phase name="#qPhases.name#" color="#qPhases.color#"/></cfloop></phases><program name=""><project id="#sProjectIdList#" startdate="#trim(getProjectStartDate(sProjectIdList))#" enddate="#trim(getProjectEndDate(sProjectIdList))#"><!--- history dates for main project (table : plmp_project_dates_history) ---><cfquery name="getNoOfHistoryVersions4Proj" datasource="#application.datasource#">select max(version_number) as version_number from plmp_project_dates_history where project_id = #sProjectIdList#</cfquery><cfif getNoOfHistoryVersions4Proj.version_number gt 0><cfloop from="1" to="#getNoOfHistoryVersions4Proj.version_number#" index="version_number">#getProjectDates(sProjectIdList, version_number)#</cfloop></cfif><!--- current dates for main project (table : project_dates) --->#getProjectDates(sProjectIdList)#<!--- Fetch sub projects ---><cfinvoke component="#ftml_oSql#" method="getSubProjectIDs" returnvariable="qProjects"><cfinvokeargument name="sProjectIdList" value="#sProjectIdList#"></cfinvoke><cfif qProjects.recordcount neq 0><cfloop query="qProjects"><subproject id="#project_id#" name="#qProjects.name#" startdate="#trim(getProjectStartDate(project_id))#" enddate="#trim(getProjectEndDate(project_id))#"><!--- history dates for sub project (table : plmp_project_dates_history) ---><cfquery name="getNoOfHistoryVersions4SubProj" datasource="#application.datasource#">select max(version_number) as version_number from plmp_project_dates_history where project_id = #project_id#</cfquery><cfif getNoOfHistoryVersions4SubProj.version_number gt 0><cfloop from="1" to="#getNoOfHistoryVersions4SubProj.version_number#" index="version_number">#getProjectDates4SubProj(project_id, version_number)#</cfloop></cfif><!--- current dates for sub project (table : project_dates) --->#getProjectDates4SubProj(project_id)#</subproject></cfloop></cfif></project></program></chart></cfxml></cfoutput>

<cffile action = "write" file = "#application.page.ROOT_PATH#temp_pco_xml\#project_id#.xml" output = "#toString(MyXMLDoc)#">

2) I need proper XML data in notepad. The notepad data is input to a flash file. Right now flash is not working well as the data in notepad is not proper XML. So I want to perform above 2 operations.

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
Advocate ,
Dec 09, 2009 Dec 09, 2009

Hi,

Place this statement,

<cfprocessingdirective suppressWhiteSpace = "Yes">

at top.

and also make sure that you are trimming every value before writing it.

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 09, 2009 Dec 09, 2009
LATEST

Daverms,

Thanks for the quick turn around..

I have applied the solution what you suggested above (<cfprocessingdrirective suppresswhitespaces="yes"), still whitespaces are existing in my output.

The output what I am getting is,
(blue color part is my output & red color indicates whitespaces)

<?xml version="1.0" encoding="UTF-8"?>
<chart showdates="true" today="09/12/2009">
<phases>

<phase color="CCFFCC" name="Funded"/>

<phase color="CDCD67" name="Concept"/>

<phase color="99CCFF" name="Feasibility"/>

<phase color="0099FF" name="Development"/>

<phase color="0099FF" name="Development"/>

<phase color="CC99FF" name="Close-out"/>

<phase color="909090" name="Sustaining"/>

</phases>
<program name="">
<project enddate=" 01/01/2010 " id="12059" startdate=" 20/06/2003 ">

<version enddate=" 01/01/2010 " number=" 1" startdate=" 20/06/2003 ">
        <phase color="CCFFCC" currentdate="20/06/2003" name="Project Start" plandate="20/06/2003" type="phase"/>
       
        <phase color="CDCD67" currentdate="" name="Concept Closure" plandate="" type="phase"/>
       
        <phase color="99CCFF" currentdate="20/06/2003" name="Feasibility Closure" plandate="20/06/2003" type="phase"/>
       
        <phase color="F0FF00" currentdate="" name="Alpha Test" plandate="" type="milestone"/>
       
        <phase color="F0FF00" currentdate="26/07/2004" name="Beta Test" plandate="31/05/2004" type="milestone"/>
       
        <phase color="0099FF" currentdate="29/06/2005" name="Commercialization" plandate="08/12/2004" type="phase"/>
       
        <phase color="CC99FF" currentdate="24/02/2006" name="Project Closed" plandate="01/01/2010" type="phase"/>
       
        </version>
       
   
   
<subproject enddate=" 16/10/2008 " id="11809" name="espWatcher Pricing Toolkit" startdate=" 01/08/2003 ">

<version enddate=" 16/10/2008 " number=" 1" startdate=" 01/08/2003 ">
        <phase color="CCFFCC" currentdate="01/08/2003" name="Project Start" plandate="01/08/2003" type="phase"/>
       
        <phase color="99CCFF" currentdate="" name="Feasibility Closure" plandate="" type="phase"/>
       
        <phase color="0099FF" currentdate="15/06/2005" name="Commercialization" plandate="08/12/2004" type="phase"/>
       
        <phase color="CC99FF" currentdate="16/10/2008" name="Project Closed" plandate="16/10/2008" type="phase"/>
       
        </version>
       
</subproject>

<subproject enddate=" 31/12/2070 " id="35704" name="espWatcher version 2 (2005)" startdate=" 01/01/2005 ">

<version enddate=" 31/12/2070 " number=" 1" startdate=" 01/01/2005 ">
        <phase color="CCFFCC" currentdate="01/01/2005" name="Project Start" plandate="01/01/2005" type="phase"/>
       
        <phase color="99CCFF" currentdate="01/07/2005" name="Feasibility Closure" plandate="01/07/2005" type="phase"/>
       
        <phase color="0099FF" currentdate="31/03/2006" name="Commercialization" plandate="31/03/2006" type="phase"/>
       
        <phase color="CC99FF" currentdate="31/12/2070" name="Project Closed" plandate="31/12/2070" type="phase"/>
       
        </version>
       
</subproject>

</project>
</program>
</chart>


However this solution removes most of the whitespaces, I want exact output as flash file is expecting so..


Where ever I am calling the CF functions, there I am getting the whitespaces. like below cases.

startdate="#getProjectStartDate(sProjectIdList)#" -> output I am getting for this statement is -> " 12/09/2009 "

Please assist me...

Regards,
Manoz.

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