CSV to XML Java

CSV (Comma Separated Values) file contains data values separated with commas. Each line in a CSV is a new record. CSV files are commonly used to transfer data from one storage system to another. In certain cases, we may need to export data from CSV to XML. XML files allow storing and transporting data without being dependent on software or hardware tools. In this article, we will learn how to convert a CSV file to XML format using Java.

The following topics shall be covered in this article:

  1. CSV to XML Converter Java API
  2. Convert CSV to XML
  3. Convert CSV to XML with Options

CSV to XML Converter Java API

For converting CSV to XML, we will be using the Aspose.Cells for Java API. It is a feature-rich, powerful and easy-to-use API to work with Microsoft Excel-associated worksheets within the Java applications. It allows creating, editing, converting and rendering the Microsoft Excel formats to different formats.

The Workbook class of the API represents an Excel spreadsheet with one or multiple worksheets. It provides various methods to open and save the native Excel files. It also enables copying data from other Workbooks, combining two Workbooks, and protecting the Excel spreadsheet. The save() method of this class allows saving the workbooks in the specified formats. The TxtLoadOptions class of the API represents the options for loading text or CSV files.

Please either downloads the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.

<repository>  
    <id>AsposeJavaAPI</id>  
    <name>Aspose Java API</name>  
    <url>https://repository.aspose.com/repo/</url>  
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-cells</artifactId>
    <version>22.6</version>
</dependency>

Convert CSV to XML in Java

We can easily convert CSV files to XML by following the steps given below:

  1. Load the CSV file using the Workbook class.
  2. Save the XML file using the save() method. It takes the output XML file path as an argument.

The code example given below shows how to convert a CSV file to XML using Java.

Convert CSV to XML in Java

Convert CSV to XML with Options in Java

We can convert CSV files to XML using advanced options by following the steps given below:

  1. Firstly, create an instance of the TxtLoadOptions class.
  2. Next, specify various load options.
  3. After that, load the CSV file using the Document class with the TxtLoadOptions object.
  4. Save the XML file using the save() method. It takes the output file path as an argument.

The code example given below shows how to convert a CSV to XML with advanced options using Java.

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to convert CSV to XML format programmatically in Java. We have also seen how to specify various load options while converting CSV to XML. Besides, you can learn more about Aspose.Cells for Java API using the documentation. In case of any ambiguity, please feel free to contact us on forum.

See Also