Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

java - XSL for converting XML to CSV : Adding Quotes to the end based on data field

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 my data is having "" and ','.

Here is my sample XML:

<record>
<column name="ID">537316</column>
<column name="TYPE">MANUAL</column>
<column name="SECONDID">546</column>
<column name="INFO">"THIS","IS",FOR,"TEST"</column>
<column name="KEY">345</column>
</record>

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);
}
}

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:text>"</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>"</xsl:text>
<xsl:if test="position() != last()">,</xsl:if>
<xsl:if test="position() = last()">
<xsl:text>&#xD;</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:text>"</xsl:text><xsl:value-of  Select="normalize-space(.)"/><xsl:text>"</xsl:text>,</xsl:if>
<xsl:if test="position() = last()"><xsl:text>"</xsl:text><xsl:value-of select="normalize-space(.)"/><xsl:text>"</xsl:text><xsl:text>&#xD;</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

The sample output should be:

ID,TYPE,SECONDID,INFO,KEY
"537316","MANUAL","546","THIS"",""IS"",FOR,""TEST""","345"

But the output I am getting is:

ID,TYPE,SECONDID,INFO,KEY

"537316","MANUAL","546",""THIS","IS",FOR,"TEST"","345"

The XML I am using is from Database and contains special character(") which is causing unexpected result(As I open the output CSV using MS Excel) in my output CSV. I need to validate data the for quotes and if there are quotes I has to add extra quotes for getting the desired output. Could someone please help me with the if condition that I can use in my XSL for validating the string and searching for ("") in the data.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
class XmlToCsv {
private static void emitHeaders( Node record ){
    NodeList fields = record.getChildNodes();
    String del = "";
    for( int iField = 0; iField < fields.getLength(); iField++ ){
        Node node = fields.item( iField );
        if( ! ( node instanceof Element ) ) continue;
        System.out.print( del );
        System.out.print( ((Element)node).getAttribute("name") );
        del = ",";
    }
    System.out.println();
}
private static void emitData( Node record ){
    NodeList fields = record.getChildNodes();
    String del = "";
    for( int iField = 0; iField < fields.getLength(); iField++ ){
        Node node = fields.item( iField );
        if( ! ( node instanceof Element ) ) continue;
        System.out.print( del );
        String cont = node.getTextContent();
        cont = cont.replaceAll( """, """" );
        System.out.print( '"' + cont + '"' );
        del = ",";
    }
    System.out.println();
}

public static void main(String args[]) throws Exception {
    File xmlSource = new File("test.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(xmlSource);
    Source source = new DOMSource(document);
    Element table = document.getDocumentElement();
    NodeList records = table.getElementsByTagName("record");
    emitHeaders( records.item( 0 ) );
    for( int iRec = 0; iRec < records.getLength(); iRec++ ){
        emitData( records.item( iRec ) );
    }
}
}

It would be even simpler using JAXB.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...