generate method
Generates XML from data using key metadata for layout preservation.
data - The JSON data map to convert. Expected to contain a single
root element key with its content as the value.
keyMeta - The key-level metadata for layout information
prettyPrint - Whether to format XML with indentation (default: true)
Returns an XML string with preserved layout from the original document.
Implementation
String generate(
Map<String, dynamic> data, {
Map<String, dynamic>? keyMeta,
bool prettyPrint = true,
}) {
if (data.isEmpty) {
return '';
}
final builder = XmlBuilder();
builder.processing(
TurboConstants.xmlProcessingInstruction,
TurboConstants.xmlDeclaration,
);
// Data should have a single root element key
for (final entry in data.entries) {
final rootKey = entry.key;
final rootValue = entry.value;
final rootMeta = _getKeyMetadata(keyMeta, rootKey);
_buildElement(
builder,
rootKey,
rootValue,
rootMeta,
keyMeta,
);
}
final document = builder.buildDocument();
return prettyPrint
? document.toXmlString(pretty: true)
: document.toXmlString();
}