• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

XML FILE TO CSV FILE

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic