Working with XML Processing

Working with XML Processing

Working with XML Processing

Aspose.Cells FOSS for C++ uses a lightweight internal XML layer to read and write the Open XML parts that make up an XLSX file. The XmlDocument, XmlElement, and XmlAttribute classes form the core of this layer.


Loading an XML Document

Use XmlDocument::Load to parse an XML string or file into the in-memory document tree:

#include "aspose/cells_foss/XmlDocument.h"

using namespace Aspose::Cells_FOSS;

// Load from string content
XmlDocument doc;
doc.Load("<root><item key=\"a\">value</item></root>");

Building and Saving XML

Use XmlDocument::Build to construct an XML document programmatically, and XmlDocument::SaveToUtf8 to serialize it back to a UTF-8 string:

XmlDocument doc;
// Build the document structure
auto xml = doc.SaveToUtf8();

Traversing Elements

XmlElement provides lightweight handle-based access to element values, children, and attributes. Default-constructed elements return IsNull() == true, which can be used as a safe null-check:

XmlElement root = doc.GetRootElement();
if (!root.IsNull()) {
    // Traverse children via GetAttributes
    auto attrs = root.GetAttributes();
}

Working with Attributes

XmlAttribute provides access to attribute name and value pairs on an element. Use XmlAttribute::MakeAttribute(name, value) to create new attributes:

XmlAttribute attr = XmlAttribute::MakeAttribute("key", "value");
bool isNull = attr.IsNull();

Internal XML Mappers

The library uses dedicated mapper classes to convert in-memory spreadsheet model objects to and from their XML representations. These are internal to the serialization layer:

Mapper ClassResponsibility
WorkbookXmlMapperReads and writes the workbook relationship part
WorksheetXmlMapperReads and writes individual worksheet XML parts
StylesheetXmlMapperReads and writes the styles part
SharedStringTableXmlMapperReads and writes the shared strings table

Each mapper exposes Read and Write methods. These are called internally by XlsxWorkbookSerializer and are not intended for direct use in application code.


Error Handling

XML parsing errors throw XmlParsingException. Catch this exception when loading untrusted or malformed XML:

try {
    doc.Load(xmlContent);
} catch (const XmlParsingException& ex) {
    // Handle parse error
}

Tips and Best Practices

  • Check XmlElement::IsNull() before accessing element data — elements at missing paths return a null handle.
  • Use XmlDocument::SaveToUtf8() to obtain a serialized string for debugging or logging intermediate XML state.
  • Do not instantiate mapper classes (WorkbookXmlMapper, WorksheetXmlMapper, etc.) directly in application code — use Workbook::Save and Workbook(filePath) instead.
  • XmlParsingException is thrown on malformed XML; always wrap external XML loads in a try/catch block.

Common Issues

IssueCauseFix
IsNull() returns trueElement not found at expected pathVerify XML structure before traversal
XmlParsingException on loadMalformed or non-UTF-8 XMLValidate the input XML before passing to Load
Mapper output differs from inputModel mutation before saveEnsure all model changes are committed before calling Save

API Reference Summary

Class/MethodDescription
XmlDocument::Load(content)Parse an XML string
XmlDocument::Build()Construct a document programmatically
XmlDocument::SaveToUtf8()Serialize the document to a UTF-8 string
XmlElement::IsNull()Check if the element handle is valid
XmlElement::GetValue()Get the text content of the element
XmlElement::GetAttributes()Get the attribute collection
XmlAttribute::IsNull()Check if the attribute handle is valid
XmlAttribute::MakeAttribute(name, value)Create a new attribute handle
XmlParsingExceptionThrown on malformed XML input
WorksheetXmlMapperInternal worksheet XML read/write
WorkbookXmlMapperInternal workbook XML read/write

See Also