How can we convert XML file to CSV?

后端 未结 6 484
星月不相逢
星月不相逢 2021-01-29 09:05

I am having an XML file



    
        
        

        
相关标签:
6条回答
  • 2021-01-29 09:42

    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.

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

    generates a newline. for example.

    0 讨论(0)
  • 2021-01-29 09:48

    Using XSLT is often a bad idea. Use 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). This class would have a addCol() method which would be called for each <COLn> with the contents of that tag.

    0 讨论(0)
  • 2021-01-29 09:52

    Read the XML file in.

    Loop throught each record and add it to a csv file.

    0 讨论(0)
  • 2021-01-29 09:55

    Use the straightforward SAX API via the standard Java JAXP package. This will allow you to write a class that receives events for each XML element your reader encounters.

    Briefly:

    1. read your XML in using SAX
    2. record text values via the SAX DefaultHandler characters() method
    3. when you get an end event for a COL, record this string value
    4. when you get the ROW end event, simply write out a comma separated line of previously recorded values
    0 讨论(0)
  • 2021-01-29 09:57

    In 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:

      <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 (e.g. TAB or a pipe symbol) is also an option.

    0 讨论(0)
  • 2021-01-29 10:08

    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.

    0 讨论(0)
提交回复
热议问题