My objective is to create a single CSV file with the data from XML files. In a folder, I have multiple XML files. The number of XML files will vary based on a day. Should I combine all the XML files first and then create the CSV or create CSV for each and then combine?

An example in Java would be really helpful. I can post a sample of XML file if needed. All the XML files are the same format.

有帮助吗?

解决方案

I'd generate CSV per XML file first, then concatenate the CSV output; concatenating the Xml files would result in invalid XML unless you created a new root document element.

In "plain" java, you're essentially going to need to parse each XML document into memory and select which fields you want to output as CSV, and write that output code.

However, this is an ideal job for XSLT, which can be invoked either from Java or from an ant task. An XSL template in text mode can select values from the XML document and write them out as CSV.

This has been described here: XML to CSV Using XSLT

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top