Skip to main content
December 8, 2009
Question

How to remove white spaces from XML content using Coldfusion?

  • December 8, 2009
  • 1 reply
  • 2801 views

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.

    This topic has been closed for replies.

    1 reply

    Inspiring
    December 8, 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

    December 8, 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.

    Inspiring
    December 8, 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?.