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

XML Question

Explorer ,
Feb 08, 2007 Feb 08, 2007

Copy link to clipboard

Copied

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.XmlText#<br>
</CFLOOP></p>
</CFLOOP>

</CFOUTPUT>

TOPICS
Advanced techniques

Views

287

Translate

Translate

Report

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

correct answers 1 Correct answer

Advocate , Feb 08, 2007 Feb 08, 2007
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.

Votes

Translate

Translate
Advocate ,
Feb 08, 2007 Feb 08, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
Explorer ,
Feb 08, 2007 Feb 08, 2007

Copy link to clipboard

Copied

That worked great, thanks!

Votes

Translate

Translate

Report

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
Participant ,
May 23, 2007 May 23, 2007

Copy link to clipboard

Copied

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].??

Votes

Translate

Translate

Report

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
LEGEND ,
May 23, 2007 May 23, 2007

Copy link to clipboard

Copied

LATEST
> 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

Votes

Translate

Translate

Report

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
Documentation