Reputation: 25
I am trying to convert a XML file to CSV file dynamically using Java code. I am able to obtain the data converted to CSV but the problem is with the Header row.I need to add the first row to the CSV file having the name of that column.
Here is the Java code:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
class xmltocsv {
public static void main(String args[]) throws Exception {
File stylesheet = new File("C:/testxsl.xsl");
File xmlSource = new File("C:/test.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlSource);
StreamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(stylesource);
Source source = new DOMSource(document);
Result outputTarget = new StreamResult(new File("c:/output.csv"));
transformer.transform(source, outputTarget);
}
}
And here is my sample XML:
<record>
<column name="ID">537316</column>
<column name="TYPE">MANUAL</column>
<column name="SECONDID" />
<column name="KEY">345</column>
</record>
Here is my XSL file:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/*">
<xsl:for-each select="*[1]/*">
<xsl:value-of select="name()"/>
<xsl:if test="position() != last()">, </xsl:if>
<xsl:if test="position() = last()">
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
<xsl:output method="text" encoding="iso-8859-1"/>
<xsl:param name="fieldNames" select="'yes'" />
<xsl:strip-space elements="*" />
<xsl:template match="/*/child::*">
<xsl:for-each select="child::*">
<xsl:if test="position() != last()"><xsl:value-of select="normalize-space(.)"/>, </xsl:if>
<xsl:if test="position() = last()"><xsl:value-of select="normalize-space (.)"/><xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The sample output should be:
ID,TYPE,SECONDID,KEY\n
537316,MANUAL,,345\n
The problem is, the XML file is a database output and will change dynamically,So I need to obtain the tag name dynamically.
Here using <xsl:value-of select="name()"/>
I am getting the tag name as column insted of ID,TYPE etc. Is there any other method other than name() so that I can get the correct tag name from the XML.
Upvotes: 1
Views: 953
Reputation: 117073
Is there any other method other than name() so that I can get the correct tag name from the XML.
You don't want the tag name, you want the value of the name
attribute. This can be returned by:
<xsl:value-of select="@name"/>
Upvotes: 3