Convert XML as a data source to a Spreadsheet, CSV or JSON

GroupDocs.Conversion allows you to use XML as a structured data source and convert it into various formats such as Spreadsheet (XLSX), CSV, or JSON. This flexibility is beneficial for analyzing data, processing it, or integrating it into other applications. Below are examples for each type of conversion implemented in Java.

Convert XML to Spreadsheet (XLSX)

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.SpreadsheetConvertOptions;
import com.groupdocs.conversion.options.load.XmlLoadOptions;

public class ConvertXmlToSpreadsheet {
    public static void convert() {
        // Initialize the Converter with XML load options
        XmlLoadOptions loadOptions = new XmlLoadOptions();
        loadOptions.setUseAsDataSource(true);

        // Initialize the Converter for the input file
        try(Converter converter = new Converter("data.xml", ()-> loadOptions) {
            // Configure spreadsheet conversion options
            SpreadsheetConvertOptions options = new SpreadsheetConvertOptions();

            // Perform the conversion
            converter.convert("converted.xlsx", options);
        }
    }

    public static void main(String[] args){
        convert();
    }
}

data.xml is sample file used in this example. Click here to download it.

converted.xlsx is converted XLSX document. Click here to download it.

Convert XML to CSV

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.filetypes.SpreadsheetFileType;
import com.groupdocs.conversion.options.convert.SpreadsheetConvertOptions;
import com.groupdocs.conversion.options.load.XmlLoadOptions;

public class ConvertXmlToCsv {
    public static void convert() {
        // Initialize the Converter with XML load options
        XmlLoadOptions loadOptions = new XmlLoadOptions();
        loadOptions.setUseAsDataSource(true);

        // Initialize the Converter for the input file
        try(Converter converter = new Converter("sample.xml", ()-> loadOptions) {
            // Configure spreadsheet conversion options for CSV format
            SpreadsheetConvertOptions options = new SpreadsheetConvertOptions();
            options.setFormat(SpreadsheetFileType.Csv);

            // Perform the conversion
            converter.convert("converted.csv", options);
        }
    }

    public static void main(String[] args){
        convert();
    }
}

data.xml is sample file used in this example. Click here to download it.

converted.csv is converted CSV document. Click here to download it.

Convert XML to JSON

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.WebConvertOptions;
import com.groupdocs.conversion.filetypes.WebFileType;
import com.groupdocs.conversion.options.load.XmlLoadOptions;

public class ConvertXmlToJson {
    public static void convert() {
        // Initialize the Converter with XML load options
        XmlLoadOptions loadOptions = new XmlLoadOptions();
        loadOptions.setUseAsDataSource(true);

        // Initialize the Converter for the input file
        try(Converter converter = new Converter("data.xml", ()-> loadOptions) {
            // Configure web conversion options for JSON format
            WebConvertOptions options = new WebConvertOptions();
            options.setFormat(WebFileType.Json);

            // Perform the conversion
            converter.convert("converted.json", options);
        }
    }

    public static void main(String[] args){
        convert();
    }
}

data.xml is sample file used in this example. Click here to download it.

converted.json is converted JSON file. Click here to download it.

Key Notes:

XML as a Data Source:

The XmlLoadOptions.setUseAsDataSource(true) treats the XML file as a structured data source, making it suitable for tabular formats (like Spreadsheet or CSV) or hierarchical formats (like JSON).

Conversion Options:

Output File Formats:

  • XLSX for Excel spreadsheets.
  • CSV for comma-separated values.
  • JSON for structured web-friendly data.

These examples allow seamless integration of XML data into your workflows, whether for analysis, reporting, or web development.