aspose file tools
The moose likes XML and Related Technologies and the fly likes XML FILE TO CSV FILE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML FILE TO CSV FILE" Watch "XML FILE TO CSV FILE" New topic


sheetal bhandari

Joined: Aug 26, 2001
Posts: 4
I have to write a generic class whic can convert any XML file into a comma separated file .The structure of XML file is not known.Can you help me out in this
Abhijit Kulkarni

Joined: Dec 03, 2000
Posts: 23

What a gr8 timing to post this question.
I just used JAXP to execute a similar job. Let me explain how?
My requirement was to find out unique keys from 31 text files...representing 30 days
e.g. key value
" 5"
"URL= 2"
and now write a .CSV file so that the unique keys were on the Y-axis and the values on the X-axis each one under the date for the month..

now the xml file had a structure like
<url value="">
<url value="">
and I loaded this file as a dom tree.
read each node, got its child node, wrote the value of the attribute "value" to a text file and wrote the values of all the text nodes below the url node seperated by commas after it.
I iterated till the end of the tree and thats it!
It was all quite recursive.
Want to know more...get in touch with me!
abhijit from pune.
Ability alone is not enough for success,it must be sparked by ambition and sustained with determination
[This message has been edited by Abhijit Kulkarni (edited August 28, 2001).]
[This message has been edited by Abhijit Kulkarni (edited August 28, 2001).]
[This message has been edited by Abhijit Kulkarni (edited August 28, 2001).]

<b><i>Abhijit</i></b> <br />---------------------------------<br />Give me my share of smile!
Glen Jansen

Joined: Apr 07, 2001
Posts: 14
I had a similar situation where I needed to generate a text file from the data inside an XML document. The solution implemented used XSLT to perform the transformation from XML to a CSV. This is returned as a String object which can be written to a file. You can also modify the method below to output directly to a file.
Here are the pieces:
XSLT snippets:
<xsl utput method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">

<xsl:for-each select="LineList/Lines">
<xsl:apply-templates select="*"/>

<xsl:template match="*">
<!-- No comma on the last element -->
<xsl:when test="position()=last()"><xsl:value-of select="."/></xsl:call-template></xsl:when>
<!-- Comma required. Not the last element -->
<xsl therwise><xsl:value-of select="."/>,</xsl therwise>
Java Snippet:
// Method to convert an XML document object to an String
private static String transformXMLToString( String styleSheet, Document orig ) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

try {
Transformer transformer = factory.newTransformer( new StreamSource( styleSheet ) );
StreamResult result = new StreamResult( baos );
transformer.transform(new DOMSource( orig ), result);
}catch(TransformerConfigurationException e ){
System.out.println("TransformerConfigurationException: " + e.getMessage() );
}catch(TransformerException e){
System.out.println("TransformerException: " + e.getMessage() );

return baos.toString();
Pass the XSL stylesheet and the document containing the XML object to the method and a String will be returned which contains the CSV. Note: The above snippets are untested as I had to modify them for presentation in the forums. The ideas are sound and should work for you. The neat part is this is fast and reusable with different stylesheets. For example, let's say you only wanted the first 10 elements in the XML document. You can design a new stylesheet and reuse the existing method.
Hope this helps.
Glen Jansen
Java/XML Cert in Training.