Loading XML Documents with Options

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Conversion MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-conversion -- dnx GroupDocs.Conversion.Mcp --yes
Folders, licensing and more →

GroupDocs.Conversion provides the XmlLoadOptions class to offer fine-grained control over how XML documents are processed during conversion. This flexibility is particularly useful when working with XML data sources, applying XSL transformations, or handling external resources.

OptionDescription
setUseAsDataSourceSpecifies whether to treat the source XML document as a data source.
setXslFoFactoryProvides an XSL document stream to convert XML-FO using XSL Formatting Objects.
setXsltFactoryProvides an XSLT document stream to apply XSLT transformations, typically for converting to HTML.

These options enable you to customize the XML document loading process based on your specific requirements.

Convert XML as a Data Source to a Spreadsheet

This example demonstrates how to use an XML document as a data source and convert it into a spreadsheet format:

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

public class XmlToSpreadsheet {
    public static void convert() {
        // Load XML as a data source
        XmlLoadOptions loadOptions = new XmlLoadOptions();
        loadOptions.setUseAsDataSource(true);

        // Initialize the converter with XML and load options
        try(Converter converter = new Converter("data.xml", () -> loadOptions)) {

            // Conversion options for spreadsheet
            SpreadsheetConvertOptions options = new SpreadsheetConvertOptions();

            // Convert XML to XLSX
            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.

Skipping the Loading of External Resources

External resources refer to data or content that is linked from within an XML document but stored separately. This includes:

  • Document Type Definitions (DTDs) or XML Schemas (XSDs)
  • External entities
  • XSLT stylesheets
  • Linked images or multimedia content
  • Data sources referenced in the XML

Why Skip External Resources?

You may want to skip loading external resources in the following cases:

  • Unavailable Resources: When external resources are missing or inaccessible.
  • Security Concerns: To prevent XML External Entity (XXE) attacks.
  • Performance Optimization: To improve conversion speed by avoiding unnecessary external calls.

To learn more about skipping external resources, refer to the Skip Loading of External Resources article in the GroupDocs.Conversion documentation.