Convert XML file to CSV in java

后端 未结 8 882
时光说笑
时光说笑 2020-12-05 01:17

@Before There will be probably some duplicate questions suggestions, I don\'t think that is the case maybe read this first, I\'ll try to be as brief as possible. Title gives

相关标签:
8条回答
  • 2020-12-05 02:04

    This looks like a good case for using XSL. Given your basic requirements it may be easier to get at the right nodes with XSL as compared to custom parsers or serializers. The benefit would be that your XSL could target "//Item//AverageTime" or whatever nodes you require without worrying about node depth.

    UPDATE: The following is the xslt I threw together to make sure this worked as expected.

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:template match="/">
    ItemID,StartTime,EndTime,ViewItemURL,AverageTime,AveragePrice
    <xsl:for-each select="//Item">
    <xsl:value-of select="ItemID"/><xsl:text>,</xsl:text><xsl:value-of select="//StartTime"/><xsl:text>,</xsl:text><xsl:value-of select="//EndTime"/><xsl:text>,</xsl:text><xsl:value-of select="//ViewItemURL"/><xsl:text>,</xsl:text><xsl:value-of select="//AverageTime"/><xsl:text>,</xsl:text><xsl:value-of select="//AveragePrice"/><xsl:text>
    </xsl:text>
    </xsl:for-each>
    </xsl:template>
    
    </xsl:stylesheet>
    
    0 讨论(0)
  • 2020-12-05 02:04

    I am not convinced that SAX is the best approach for you. There are different ways you could use SAX here, though.

    If element order is not guaranteed within certain elements, like ListingDetails, then you need to be proactive.

    When you start a ListingDetails, initialize a map as a member variable on the handler. In each subelement, set the appropriate key-value in that map. When you finish a ListingDetails, examine the map and explicitly mock values such as nulls for the missing elements. Assuming you have one ListingDetails per item, save it to a member variable in the handler.

    Now, when your item element is over, have a function that writes the line of CSVs based on the map in the order you wanted.

    The risk with this is if you have corrupted XML. I would strongly consider setting all these variables to null when an item starts, and then checking for errors and announcing them when the item ends.

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