java 我们如何将 XML 文件转换为 CSV?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1086807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can we convert XML file to CSV?
提问by Rakesh Juyal
I am having an XML file
我有一个 XML 文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<Results>
<Row>
<COL1></COL1>
<COL2>25.00</COL2>
<COL3>2009-07-06 15:49:34.984</COL3>
<COL4>00001720</COL4>
</Row>
<Row>
<COL1>RJ</COL1>
<COL2>26.00</COL2>
<COL3>2009-07-06 16:04:16.156</COL3>
<COL4>00001729</COL4>
</Row>
<Row>
<COL1>SD</COL1>
<COL2>28.00</COL2>
<COL3>2009-07-06 16:05:04.375</COL3>
<COL4>00001721</COL4>
</Row>
</Results>
I have to convert this XML into CSV file. I have heard we can do such thing using XSLT. How can i do this in Java ( with/without XSLT )?
我必须将此 XML 转换为 CSV 文件。我听说我们可以使用 XSLT 做这样的事情。我如何在 Java 中做到这一点(有/没有 XSLT)?
回答by Vinay Sajip
Using XSLT is often a bad idea. Use Apache Commons Digester. It's fairly easy to use - here's a rough idea::
使用 XSLT 通常是一个坏主意。使用Apache Commons Digester。它相当容易使用 - 这是一个粗略的想法:
Digester digester = new Digester();
digester.addObjectCreate("Results/Row", MyRowHolder.class);
digester.addCallMethod("Results/Row/COL1","addCol", 0);
// Similarly for COL2, etc.
digester.parse("mydata.xml");
This will create a MyRowHolderinstance (where this is a class you provide). This class would have a addCol()method which would be called for each <COLn>with the contents of that tag.
这将创建一个MyRowHolder实例(这是您提供的类)。此类将有一个addCol()方法,该方法将<COLn>使用该标签的内容为每个方法调用。
回答by geowa4
In pseudo code:
在伪代码中:
loop through the rows:
loop through all children of `Row`:
write out the text
append a comma
new line
That quick little loop will write a comma at the end of each line, but I'm sure you can figure out how to remove that.
那个快速的小循环会在每一行的末尾写一个逗号,但我相信你可以弄清楚如何删除它。
For actually parsing the XML, I suggest using JDOM. It has a pretty intuitive API.
对于实际解析 XML,我建议使用JDOM。它有一个非常直观的 API。
回答by Tomalak
In XSLT 1.0:
在 XSLT 1.0 中:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="ISO-8859-1" />
<xsl:template match="/Results">
<xsl:apply-templates select="Row" />
</xsl:template>
<xsl:template match="Row">
<xsl:apply-templates select="*" />
<xsl:if test="not(last())">
<xsl:value-of select="' '" />
</xsl:if>
</xsl:template>
<xsl:template match="Row/*">
<xsl:value-of select="." />
<xsl:if test="not(last())">
<xsl:value-of select="','" />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
If your COL* values can contain commas, you could wrap the values in double quotes:
如果您的 COL* 值可以包含逗号,您可以将这些值用双引号括起来:
<xsl:template match="Row/*">
<xsl:value-of select="concat('"', ., '"')" />
<!-- ... --->
If they can contain commas anddouble quotes, things could get a bit more complex due to the required escaping. You know your data, you'll be able to decide how to best format the output. Using a different separator (e.g. TAB or a pipe symbol) is also an option.
如果它们可以包含逗号和双引号,由于需要转义,事情可能会变得更加复杂。您知道您的数据,您将能够决定如何最好地格式化输出。使用不同的分隔符(例如 TAB 或管道符号)也是一种选择。
回答by Brian Agnew
Use the straightforward SAXAPI via the standard Java JAXPpackage. This will allow you to write a class that receives events for each XML element your reader encounters.
通过标准 Java JAXP包使用简单的SAXAPI 。这将允许您编写一个类来接收读者遇到的每个 XML 元素的事件。
Briefly:
简要地:
- read your XML in using SAX
- record text values via the SAX DefaultHandlercharacters() method
- when you get an end event for a COL, record this string value
- when you get the ROW end event, simply write out a comma separated line of previously recorded values
- 使用 SAX 读取您的 XML
- 通过 SAX DefaultHandlercharacters() 方法记录文本值
- 当您收到 COL 的结束事件时,记录此字符串值
- 当您收到 ROW 结束事件时,只需写出之前记录值的逗号分隔行
回答by Gerco Dries
With XSLT you can use the JAXP interface to the XSLT processor and then use <xsl:text> in your stylesheet to convert to text output.
使用 XSLT,您可以使用 XSLT 处理器的 JAXP 接口,然后使用样式表中的 <xsl:text> 转换为文本输出。
<xsl:text> </xsl:text>
generates a newline. for example.
生成换行符。例如。
回答by Derek Organ
Read the XML file in.
读入 XML 文件。
Loop throught each record and add it to a csv file.
循环遍历每条记录并将其添加到 csv 文件中。

