Skip to main content
October 28, 2010
Question

creating XML

  • October 28, 2010
  • 3 replies
  • 4300 views

I want to build an xml file from a DB. I see how to do it but cannot get it the way it should be:

<updates>

     <update> date="27-10-2010" label="some text here"/>

</updates>

When I try and recreate the above layout I cannot get the line to read correctly. I use my query:

<cfquery name="messages" datasource="myData">

SELECT *

FROM myMessages

</cfquery>

<cfoutput query="messages">

<update label="#XMLFormat(message)#"/>

</cfoutput>

This produces nothing on my page. But if I put a space bfor the update:

<cfoutput query="messages">

< update label="#XMLFormat(message)#"/>

</cfoutput>

I get this:

< update label="this is a test message" />
< update label="this is another one" />

This is very close to what I want but the xml cannot be read if there is a space. What can I do? Any thoughts would be great

Thanks

George

    This topic has been closed for replies.

    3 replies

    BKBK
    Community Expert
    Community Expert
    October 31, 2010

    <cfsavecontent variable="myXml">


    <cfoutput query="messages">
    <update label="#message#"/>
    </cfoutput>
    </cfsavecontent>

    <cfoutput>#xmlFormat(myXml)#</cfoutput>
    Inspiring
    October 31, 2010

    I'm curious - there have been a couple of postings lately on "how to create XML in CF" and the suggested code always points to using CFSAVECONTENT and not CFXML.  I have always used CFXML, partly because it will validate the document for me and therefore I know that I've generated a good piece of XML, whereas CFSAVECONTENT just builds a text string.  When it's time to send it out to a webservice, I just wrap the var name in toString(). If I CFDUMP the XML object I get a nicely structured output.  Am I missing something?

    -reed

    Inspiring
    October 31, 2010

    I usually use CFSAVECONTENT or just CFOUTPUT to the browser depending on my needs. I generally avoid CFXML. CFXML parses the XML string into an object that can be used by other XML related functions, such as for Xpath queries using the XmlSearch function. This uses more server resources than simply creating text. If you are just outputing data as an XML string or to a text file and don't need to do any Xpath queries or other querying of the XML data set there is no reason (in my opinion) to add the extra overhead of using CFXML.

    Inspiring
    October 28, 2010

    Here is a quick sample.

    <cfquery name="messages" datasource="myData">

    SELECT MessageDate, MessageLabel

    FROM myMessages

    </cfquery>

    <!--- use CFCONTENT to output content as XML rather than HTML, do not leave any space between CFCONTENT tag and XML declaration --->

    <cfcontent type="text/xml; charset=utf-8" reset="yes"><?xml version="1.0" ?><updates>

    <cfouput query="messages">

        <update date="#XmlFormat(MessageDate)#" label="#XmlFormat(MessageLabel)#" />

    </cfoutput>

    </updates>

    Message was edited by: JR "Bob" Dobbs

    ilssac
    Inspiring
    October 28, 2010

    GeorgeWS wrote:

    This produces nothing on my page.

    This is because browsers are trained to never show anything found inside angle brackets [<....>].  I,E. HTML or XML tags.

    Your first example was probably working fine, though you are missing your root element.  But you needed to "View Source" to see all the XML that the browser was deliberatly NOT showing.

    The other way to do it is to htmlEditFormat() or htmlCodeFormat() your entire XML content.  Which would properly escape all the angle brackes and other HTML special characters so that the browser would display them rather then ignore them.

    P.S. you could also use <cfcontent type="text/xml"...> to let the browser know if is receiving XML (and nothing but XML).  Then most of them will automatically apply a stylesheet to display the XML, if no other stylesheet is detected.

    Inspiring
    October 28, 2010

    Or you can use one of my favourite tags, cfdump.

    Upon further review, if you want to see the xml, use create your xml inside a cfsavecontent tag and then use cfdump on the resulting variable.

    Message was edited by: Dan Bracuk

    ilssac
    Inspiring
    October 28, 2010

    Dan Bracuk wrote:

    Or you can use one of my favourite tags, cfdump.

    There is one to chalk up to "something new every day."  I did not realize that <cfdump...> automatically applies an html format function to its output.