XML Serializable

Pub Version

Provides build builders for handling XML.

The builders generate code when they find members annotated with classes defined in xml_annotation.

Usage

Given a library example.dart with a Person class annotated with @XmlSerializable():

import 'package:xml/xml.dart';
import 'package:xml_annotation/xml_annotation.dart' as annotation;

part 'example.g.dart';

@annotation.XmlRootElement(name: 'person')
@annotation.XmlSerializable()
class Person {
  @annotation.XmlElement(name: 'firstname')
  final String firstName;

  @annotation.XmlElement(name: 'lastname')
  final String lastName;

  Person({this.firstName, this.lastName});

  factory Person.fromXmlElement(XmlElement element) =>
      _$PersonFromXmlElement(element);

  void buildXmlChildren(
    XmlBuilder builder, {
    Map<String, String> namespaces = const {},
  }) =>
      _$PersonBuildXmlChildren(
        this,
        builder,
        namespaces: namespaces,
      );

  void buildXmlElement(
    XmlBuilder builder, {
    Map<String, String> namespaces = const {},
  }) =>
      _$PersonBuildXmlElement(
        this,
        builder,
        namespaces: namespaces,
      );

  List<XmlAttribute> toXmlAttributes({
    Map<String, String> namespaces = const {},
  }) =>
      _$PersonToXmlAttributes(
        this,
        namespaces: namespaces,
      );

  List<XmlNode> toXmlChildren({
    Map<String, String> namespaces = const {},
  }) =>
      _$PersonToXmlChildren(
        this,
        namespaces: namespaces,
      );

  XmlElement toXmlElement({
    Map<String, String> namespaces = const {},
  }) =>
      _$PersonToXmlElement(
        this,
        namespaces: namespaces,
      );
}

Building creates the corresponding part example.g.dart:

part of 'example.dart';

void _$PersonBuildXmlChildren(
  Person instance,
  XmlBuilder builder, {
  Map<String, String> namespaces = const {},
}) {
  builder.element(
    'firstname',
    nest: () {
      if (instance.firstName != null) {
        builder.text(
          instance.firstName,
        );
      }
    },
  );
  builder.element(
    'lastname',
    nest: () {
      if (instance.lastName != null) {
        builder.text(
          instance.lastName,
        );
      }
    },
  );
}

void _$PersonBuildXmlElement(
  Person instance,
  XmlBuilder builder, {
  Map<String, String> namespaces = const {},
}) {
  builder.element(
    'person',
    namespaces: namespaces,
    nest: () {
      instance.buildXmlChildren(
        builder,
        namespaces: namespaces,
      );
    },
  );
}

Person _$PersonFromXmlElement(XmlElement element) {
  final firstName = element.getElement(
    'firstname',
  );
  final lastName = element.getElement(
    'lastname',
  );

  return Person(
    firstName: firstName != null ? firstName.text : null,
    lastName: lastName != null ? lastName.text : null,
  );
}

List<XmlAttribute> _$PersonToXmlAttributes(
  Person instance, {
  Map<String, String> namespaces = const {},
}) {
  return [];
}

List<XmlNode> _$PersonToXmlChildren(
  Person instance, {
  Map<String, String> namespaces = const {},
}) {
  return [
    XmlElement(
      XmlName(
        'firstname',
      ),
      [],
      [
        if (instance.firstName != null)
          XmlText(
            instance.firstName,
          ),
      ],
    ),
    XmlElement(
      XmlName(
        'lastname',
      ),
      [],
      [
        if (instance.lastName != null)
          XmlText(
            instance.lastName,
          ),
      ],
    ),
  ];
}

XmlElement _$PersonToXmlElement(
  Person instance, {
  Map<String, String> namespaces = const {},
}) {
  return XmlElement(
    XmlName(
      'person',
    ),
    [
      for (final entry in namespaces.entries)
        XmlAttribute(
          entry.value != null
              ? XmlName(
                  entry.value,
                  'xmlns',
                )
              : XmlName(
                  'xmlns',
                ),
          entry.key,
        ),
      ...instance.toXmlAttributes(
        namespaces: namespaces,
      ),
    ],
    instance.toXmlChildren(
      namespaces: namespaces,
    ),
  );
}

Features and bugs

Please file feature requests and bugs at the issue tracker.

Libraries

xml_serializable
xml_serializable.builder
Configuration for using package:build-compatible build systems. [...]