Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML FILE TO CSV FILE

 
sheetal bhandari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
-------------------------------------------------------------
"http://www.abhijit.com 5"
"URL=http://www.you.com 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..
e.g
---------------------.CSV----------------------------------------
URL,1,2,3,4,5,6,7,8,9,10...
'http://www.abhijit.com,5,2,3,4,5,6,7....'
'http://www.you.com,2,3,4,5,6,7....'

now the xml file had a structure like
<month>
<url value="http://www.abhijit.com">
<day1>5</day1>
<day2>2</day2>
<day3>1</day3>
<day4>5</day4>
<day5>7</day5>
<day6>1</day6>
.
.
</url>
<url value="http://www.you.com">
<day1>2</day1>
.
.
</url>
</month>
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
------------------
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).]
 
Glen Jansen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:for-each>

</xsl:template>
<xsl:template match="*">
<xsl:choose>
<!-- 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>
</xsl:choose>
</xsl:template>
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic