Convert XML as a data source to a Spreadsheet, CSV or JSON
Convert XML as a data source to a Spreadsheet, CSV or JSON
Leave feedback
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)
importcom.groupdocs.conversion.Converter;importcom.groupdocs.conversion.options.convert.SpreadsheetConvertOptions;importcom.groupdocs.conversion.options.load.XmlLoadOptions;publicclassConvertXmlToSpreadsheet{publicstaticvoidconvert(){// Initialize the Converter with XML load options
XmlLoadOptionsloadOptions=newXmlLoadOptions();loadOptions.setUseAsDataSource(true);// Initialize the Converter for the input file
try(Converterconverter=newConverter("data.xml",()->loadOptions){// Configure spreadsheet conversion options
SpreadsheetConvertOptionsoptions=newSpreadsheetConvertOptions();// Perform the conversion
converter.convert("converted.xlsx",options);}}publicstaticvoidmain(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
importcom.groupdocs.conversion.Converter;importcom.groupdocs.conversion.filetypes.SpreadsheetFileType;importcom.groupdocs.conversion.options.convert.SpreadsheetConvertOptions;importcom.groupdocs.conversion.options.load.XmlLoadOptions;publicclassConvertXmlToCsv{publicstaticvoidconvert(){// Initialize the Converter with XML load options
XmlLoadOptionsloadOptions=newXmlLoadOptions();loadOptions.setUseAsDataSource(true);// Initialize the Converter for the input file
try(Converterconverter=newConverter("sample.xml",()->loadOptions){// Configure spreadsheet conversion options for CSV format
SpreadsheetConvertOptionsoptions=newSpreadsheetConvertOptions();options.setFormat(SpreadsheetFileType.Csv);// Perform the conversion
converter.convert("converted.csv",options);}}publicstaticvoidmain(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
importcom.groupdocs.conversion.Converter;importcom.groupdocs.conversion.options.convert.WebConvertOptions;importcom.groupdocs.conversion.filetypes.WebFileType;importcom.groupdocs.conversion.options.load.XmlLoadOptions;publicclassConvertXmlToJson{publicstaticvoidconvert(){// Initialize the Converter with XML load options
XmlLoadOptionsloadOptions=newXmlLoadOptions();loadOptions.setUseAsDataSource(true);// Initialize the Converter for the input file
try(Converterconverter=newConverter("data.xml",()->loadOptions){// Configure web conversion options for JSON format
WebConvertOptionsoptions=newWebConvertOptions();options.setFormat(WebFileType.Json);// Perform the conversion
converter.convert("converted.json",options);}}publicstaticvoidmain(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).