Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Convert XML to CSV

843834Jan 13 2009 — edited Mar 3 2010
Hi All,

I have an XML file as

Stored in c:\output.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="output.xsl"?>
<catalog>
	<cd>
		<title>Empire Burlesque</title>
		<artist>Bob Dylan</artist>
		<country>USA</country>
		<company>Columbia</company>
		<price>10.90</price>
		<year>1985</year>
	</cd>
	<cd>
		<title>Hide your heart</title>
		<artist>Bonnie Tyler</artist>
		<country>UK</country>
		<company>CBS Records</company>
		<price>9.90</price>
		<year>1988</year>
	</cd>
	<cd>
		<title>Greatest Hits</title>
		<artist>Dolly Parton</artist>
		<country>USA</country>
		<company>RCA</company>
		<price>9.90</price>
		<year>1982</year>
	</cd>
	<cd>
		<title>Still got the blues</title>
		<artist>Gary Moore</artist>
		<country>UK</country>
		<company>Virgin records</company>
		<price>10.20</price>
		<year>1990</year>
	</cd>
	<cd>
		<title>Eros</title>
		<artist>Eros Ramazzotti</artist>
		<country>EU</country>
		<company>BMG</company>
		<price>9.90</price>
		<year>1997</year>
	</cd>
</catalog>
And i want to convert it to csv format so i wrote the xsl file as

Stored in c:\output.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:template match="/">
		<html>
			<body>
				<xsl:apply-templates></xsl:apply-templates>
			</body>
		</html>
	</xsl:template>

	<xsl:template match="cd">
			<xsl:apply-templates select="title"></xsl:apply-templates>
			<xsl:apply-templates select="artist"></xsl:apply-templates>
			<xsl:apply-templates select="country"></xsl:apply-templates>
			<xsl:apply-templates select="company"></xsl:apply-templates>
			<xsl:apply-templates select="price"></xsl:apply-templates>
			<xsl:apply-templates select="year"></xsl:apply-templates>
	</xsl:template>

	<xsl:template match="title">
		<xsl:value-of select="."></xsl:value-of>,
	</xsl:template>

	<xsl:template match="artist">
		<xsl:value-of select="."></xsl:value-of>,
	</xsl:template>

	<xsl:template match="country">
		<xsl:value-of select="."></xsl:value-of>,
	</xsl:template>

	<xsl:template match="company">
		<xsl:value-of select="."></xsl:value-of>,
	</xsl:template>

	<xsl:template match="price">
		<xsl:value-of select="."></xsl:value-of>,
	</xsl:template>

	<xsl:template match="year">
		<xsl:value-of select="."></xsl:value-of>
	<br></br>
	</xsl:template>

</xsl:stylesheet>
and the java code i am using is
String xsltFile = "c:\\output.xsl";
		String xmlsource = "c:\\output.xml";
		String locationOfNewContentXml = "c:\\result.csv";
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		docBuilderFactory.setIgnoringElementContentWhitespace(true);
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Source xsltSource = new StreamSource(xsltFile);
		File xmlSourceFile = new File(xmlsource);
		try
		{
			Transformer sourcetransormer = transformerFactory.newTransformer(xsltSource);
			OutputStream yourFileOutputStream = new FileOutputStream(new File(locationOfNewContentXml));
			sourcetransormer.transform(new StreamSource(xmlSourceFile),  new StreamResult(new OutputStreamWriter(yourFileOutputStream)));
			yourFileOutputStream.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
The csv file being produced contains A1 = Empire Burlesque, Bob Dylan, USA, Columbia, 10.90, 1985
and the next row in next A1 and so on.



Can anyone suggest me where am i wrong?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 31 2010
Added on Jan 13 2009
11 comments
1,873 views