Skip to main content
Participating Frequently
December 7, 2010
Question

XML Create and Export

  • December 7, 2010
  • 1 reply
  • 1205 views

Hello,

I am trying to create a XML document in coldfusion using the below method.

<cfoutput>
     <cfxml variable="MyXMLDoc">
          <?xml version='1.0' encoding='utf-8' ?>
               <root>

                    <cfloop query = "student">

                         <Student ID>           #S_ID#          </Student ID>

                         <Student Name>     #S_Name#     </Student Name>

                    </cfloop>

               </root>

     </cfxml>

</cfoutput>

The problem  I am encountering is with the XML Tag <Student ID>, ID is considered as an attribute of Student and it expects a value to be associated with ID attribute. How to address this problem?

Also, How do I export the XML document? I am able to dump the contents on the screen but if I want to export as I do for an excel doc as below

<cfcontent type="application/vnd.ms-excel">

How do I do it?

Thanks

Nikhil


    This topic has been closed for replies.

    1 reply

    Community Expert
    December 7, 2010

    First, I don't think you need the CFOUTPUT around CFXML.

    Second, you can specify attributes just like they'd be written in XML:

    <root>

         <cfloop query="student">

         <Student ID="#S_ID#" Name="#S_Name#"/>

         </cfloop>

    </root>

    Finally, since you're storing the value of the CFXML tag in a variable, you'd output it like any other variable:

    <cfcontent ... variable="#MyXMLDoc#">

    Dave Watts, CTO, Fig Leaf Software

    http://www.figleaf.com/

    http://training.figleaf.com/

    Read this before you post:

    http://forums.adobe.com/thread/607238

    Dave Watts, Eidolon LLC
    Participating Frequently
    December 7, 2010

    I also have various other tags like

    <Address Line 1>

    <Address City>

    <Address State>

    <Student Phone>

    <Semester Year>

    I need some way to represent these tags.

    Thanks

    Nikhil

    Community Expert
    December 7, 2010

    Do you have an existing XML schema that you have to work with? Or are you just creating this XML out of thin air, so to speak?

    If you're just making up your own format, you can really do it any way you like. For example:

    <Student ID="..." Year="...">

         <Name>...</Name>

         <Address>

              <Street>...</Street>

              <City>...</City>

              <State>...</State>

         </Address>

         <Phone>...</Phone>

    </Student>

    Or, you could use more attributes instead of elements, etc. On the other hand, if you have an existing format that you have to follow, you just ... follow that format.

    Dave Watts, CTO, Fig Leaf Software

    http://www.figleaf.com/

    http://training.figleaf.com/

    Read this before you post:

    http://forums.adobe.com/thread/607238

    Dave Watts, Eidolon LLC