4 Replies Latest reply on May 23, 2007 4:20 PM by Newsgroup_User

    XML Question

    AdobeCF7User Level 1
      I haven't used XML before and I was given a file that looks like this:

      <DataSet>
      <productSum>
      <productTotal>2</productTotal>
      <productNew>0</productNew>
      </productSum>
      <product>
      <productId>122</productId>
      <productName>Demo Kit</productName>
      <productRelease>1.2.5</productRelease>
      <productQty>32</productQty>
      </product>
      <product>
      <productId>174</productId>
      <productName>Sample Kit</productName>
      <productRelease>2.0</productRelease>
      <productQty>3</productQty>
      </product>
      </DataSet>

      I read through http://www.adobe.com/devnet/coldfusion/articles/xmlxslt.pdf and tried to convert some sample code to read this XML file. I'm having no luck though.

      I want to be able to output to a csv file so it looks like this:

      productID,productName,productRelease,productQty
      122,Demo Kit,1.2.5,32
      174,Sample Kit,2.0,3

      How can I modify this code so I can produce the CSV? I need to get a listing of the items in the <product> tag.

      Thanks in advance!

      <CFSET MyXmlFile = ExpandPath("test.xml")>

      <CFFILE
      ACTION="READ"
      FILE="#MyXmlFile#"
      VARIABLE="MyXmlCode">

      <CFSET MyXml = XmlParse(MyXmlCode)>
      <CFSET xnDataSet = MyXML.XmlRoot>

      <CFSET nNumProducts = ArrayLen(xnDataSet.XmlChildren)>

      <CFOUTPUT>

      <p>The company has a total of: <b>#nNumProducts# products</b></p>

      <CFLOOP FROM="1" TO="#nNumProducts#" INDEX="i">
      <CFSET xnThisProduct = xnDataSet.XmlChildren >

      <CFSET arProducts = xnThisProduct["product"].XmlChildren>
      <CFSET nNumProducts = ArrayLen(arProducts)>

      <p>Product #i# (product):<br>

      <CFLOOP FROM="1" TO="#nNumProducts#" INDEX="j">
      #arProducts[j].XmlText#<br>
      </CFLOOP></p>
      </CFLOOP>

      </CFOUTPUT>

        • 1. Re: XML Question
          insuractive Level 3
          I would reccomend using XMLSearch(MyXml, "//product") - that will give you an array of your product elements. Then is just a matter of looping over the product array, then looping over the child elements of each item and outputing them. Don't underestimate the power of using <cfdump> when working with XML to get a good sense of where you are in the XML document and what elements are available to you.
          • 2. Re: XML Question
            AdobeCF7User Level 1
            That worked great, thanks!
            • 3. Re: XML Question
              Balddog Level 1
              I am so lost. Where can I find a basic tutorial on the structure of xmldocument? I want the "element" (not sure I'm even using the right verbiage) below the children.

              xmldoc.xmlchildren[2].??
              • 4. Re: XML Question
                Level 7
                > I am so lost. Where can I find a basic tutorial on the structure of xmldocument? I want the "element" (not sure I'm even using the right verbiage) below the children.

                For CF's support of XML? livedocs.adobe.com.

                For just "XML": www.w3c.org.

                For tutorials on xpath and XSL and that sort of thing:
                - www.zvon.org
                - www.w3schools.com

                --
                Adam