简体   繁体   English

我们如何将XML文件转换为CSV?

[英]How can we convert XML file to CSV?

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. 我必须将此XML转换为CSV文件。 I have heard we can do such thing using XSLT. 我听说我们可以使用XSLT进行此类操作。 How can i do this in Java ( with/without XSLT )? 如何在Java中(带有/不带有XSLT)执行此操作?

Using XSLT is often a bad idea. 使用XSLT通常不是一个好主意。 Use Apache Commons Digester . 使用Apache Commons Digester It's fairly easy to use - here's a rough idea:: 它相当容易使用-这是一个粗略的想法:

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 MyRowHolder instance (where this is a class you provide). 这将创建MyRowHolder实例(这是您提供的类)。 This class would have a addCol() method which would be called for each <COLn> with the contents of that tag. 此类将具有addCol()方法,该方法将为每个带有该标签内容的<COLn>调用。

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 . 为了实际解析XML,我建议使用JDOM It has a pretty intuitive API. 它具有非常直观的API。

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="'&#10;'" />  
    </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 and double 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 (eg TAB or a pipe symbol) is also an option. 也可以选择使用其他分隔符(例如TAB或管道符号)。

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,您可以将JAXP接口用于XSLT处理器,然后在样式表中使用<xsl:text>转换为文本输出。

<xsl:text>&#10;</xsl:text>

generates a newline. 生成换行符。 for example. 例如。

Read the XML file in. 读入XML文件。

Loop throught each record and add it to a csv file. 遍历每条记录并将其添加到csv文件。

Use the straightforward SAX API via the standard Java JAXP package. 通过标准Java JAXP软件包使用直接的SAX API。 This will allow you to write a class that receives events for each XML element your reader encounters. 这将允许您编写一个类,该类接收读者遇到的每个XML元素的事件。

Briefly: 简要地:

  1. read your XML in using SAX 使用SAX读取XML
  2. record text values via the SAX DefaultHandler characters() method 通过SAX DefaultHandler character()方法记录文本值
  3. when you get an end event for a COL, record this string value 当您收到COL的结束事件时,请记录此字符串值
  4. when you get the ROW end event, simply write out a comma separated line of previously recorded values 当您收到ROW结束事件时,只需用逗号分隔之前记录的值的行即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM