importXml top-level property

FunctionDefinition importXml
final

Implementation

final importXml = FunctionDefinition(
  name: 'importXml',
  documentation: FunctionDefinitionDocumentation(
    description:
        'Imports a XML file '
        'and decode it to a Map<String, dynamic> '
        '(e.g. to be assigned to a variable)',
    examples: [
      ExpressionBlockExample(
        (delimiter) =>
            "${delimiter.start}xml=importXml('test/src/parser/expression"
            "/function/import/person.xml')${delimiter.end}"
            "${delimiter.start}xml.person.child.name${delimiter.end}",
      ),
      TestFileExample.fromProjectFilePath(
        'test/src/parser/expression/function/import/import_xml_test.dart',
      ),
    ],
  ),

  parameters: [
    Parameter<String>(
      name: 'source',
      description:
          'The project path of the XML file. '
          'This path can be a absolute or relative file path or URI.',
      presence: PresenceMandatory(),
    ),
  ],
  function: (context) async {
    try {
      var source = context.argumentValues['source'] as String;

      var xmlText = await readSource(source);
      var xmlMap = _xmlToDataMap(xmlText);
      return xmlMap;
    } on Exception catch (e) {
      var message = e
          .toString()
          .replaceFirst('Exception: ', '')
          .replaceAll('\r', '')
          .replaceAll('\n', '');
      throw RenderError(
        'Error importing a XML file: $message',
        context.functionLocation,
      );
    }
  },
);