Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert any XML document into CSV 1

Status
Not open for further replies.

prmanju

Programmer
Jul 19, 2005
7
US
Is it possible to convert any given XML into CSV format? I know I can use XSLT to convert XML into CSV, if you know the XML DTD/Schema. But I don't know the name of tags and # of tags used in the XML.
Ex:

<ROOT>
<ROW>
<COL1>123</COL1>
<COL2>224</COL2>
<COL3>eterte</COL3>
</ROW>
</ROOT>

Need to convert into

COL1,COL2,COL3
123,224,eterte

I would really appreciate any help.

 
This is possible in XSL. You could use DOM too. I'm not entirely clear on how you want the output to be formatted. What if you have nodes on different levels of the tree?

Here's an example that will convert all the nodes that have a text node child to name/value pairs:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="text"/>
  <xsl:template match="@*|node()">
    <xsl:if test="text()">
      <xsl:value-of select="concat('&amp;', name(.), '=', text())"/>
    </xsl:if>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:template>
</xsl:stylesheet>

Jon

"I don't regret this, but I both rue and lament it.
 
Thanks Jon for your quick reply. The output I need is comma separated text file with header. The header should be the name of the final node. It is possible that the node can be at different level of the tree.

The code is returning the rows for all level node. Is it possible to display just the text node with values?

Could you please guide me to good XSL resource/tutorial?

Thanks again for your help!

 
Also Could you please tell me how to do with DOM Parser?
 
Still not sure what format you want. Give a better example. Slight change will give you CSV:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="text"/>
  <xsl:template match="@*|node()">
    <xsl:if test="text()">
      <xsl:value-of select="concat(name(.), ',', text(), ',')"/>
    </xsl:if>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:template>
</xsl:stylesheet>

Jon

"I don't regret this, but I both rue and lament it.
 
Thanks for quick response.

For Ex:
The following is the incoming XML
======================================
<?xml version="1.0"?>
<GroupLotMovement>
<Record>
<PremiseLocationID>1231</PremiseLocationID>
<USDAPremiseID>12355</USDAPremiseID>
<EventTypeCode>MOV</EventTypeCode>
<EventDateTime>12/3/2005</EventDateTime>
<LotIDNumber>Lot # 1 </LotIDNumber>
<GLSubsetID>Sub set 1</GLSubsetID>
<GroupType>GT 1 </GroupType>
<HeadCount>4</HeadCount>
<Species>COW </Species>
<EventRemark> None</EventRemark>
<Correction>None</Correction>
</Record>
<Record>
<PremiseLocationID>1232</PremiseLocationID>
<USDAPremiseID>12355</USDAPremiseID>
<EventTypeCode>MOV</EventTypeCode>
<EventDateTime>12/3/2005</EventDateTime>
<LotIDNumber>Lot # 1 </LotIDNumber>
<GLSubsetID>Sub set 1</GLSubsetID>
<GroupType>GT 1 </GroupType>
<HeadCount>4</HeadCount>
<Species>COW </Species>
<EventRemark> None</EventRemark>
<Correction>None</Correction>
</Record>
<Record>
<PremiseLocationID>1233</PremiseLocationID>
<USDAPremiseID>12334</USDAPremiseID>
<EventTypeCode>MOV</EventTypeCode>
<EventDateTime>12/3/2005</EventDateTime>
<LotIDNumber>Lot # 1 </LotIDNumber>
<GLSubsetID>Sub set 1</GLSubsetID>
<GroupType>GT 1 </GroupType>
<HeadCount>4</HeadCount>
<Species>COW </Species>
<EventRemark> None</EventRemark>
<Correction>None</Correction>
</Record>
<Record>
<PremiseLocationID>1234</PremiseLocationID>
<USDAPremiseID>12334</USDAPremiseID>
<EventTypeCode>MOV</EventTypeCode>
<EventDateTime>12/3/2005</EventDateTime>
<LotIDNumber>Lot # 1 </LotIDNumber>
<GLSubsetID>Sub set 1</GLSubsetID>
<GroupType>GT 1 </GroupType>
<HeadCount>4</HeadCount>
<Species>COW </Species>
<EventRemark></EventRemark>
<Correction></Correction>
</Record>

</GroupLotMovement>
=====================================

I need to convert into the following format (CSV)

=======================================
PremiseLocationID,USDAPremiseID,EventTypeCode,EventDateTime,LotIDNumber,GLSubsetID,GroupType,HeadCount,Species,EventRemark,Correction
1231,12355,MOV,12/3/2005,Lot # 1 ,Sub set 1,GT 1 ,4,COW , None,None
1232,12355,MOV,12/3/2005,Lot # 1 ,Sub set 1,GT 1 ,4,COW , None,None
1233,12334,MOV,12/3/2005,Lot # 1 ,Sub set 1,GT 1 ,4,COW , None,None
1234,12334,MOV,12/3/2005,Lot # 1 ,Sub set 1,GT 1 ,4,COW ,,

=======================

And the incoming XML could be in different format (for ex: different tag name, different levels...)

Hope this helps better undertanding of my problem.


 
How is this going to work if the tags can be on different levels?

Jon

"I don't regret this, but I both rue and lament it.
 
You are right. We have to make at least the following assumption.

The final level would be always tags with text nodes.

Thanks again.

 
Jon,
Do you have example to convert the XML file to CSV using DOM?

Thanks again.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top