import 'package:xml/xml.dart';
// Plant will be mapped to XML. Field annotations contain directives for the
// encoder and decoder. Here we use some special features of the XML package:
// the `XmlElement` annotation dictates the name of the XML element representing
// this class; `XmlAttribute` means that the field is an XML attribute rather
// than a nested element.
class Plant {
@XmlElement(name: 'plant')
final String name;
@XmlAttribute(name: 'id')
final int id;
@XmlElement(name: 'origin')
final List<String> origin;
Plant(this.id, this.name, this.origin);
@override
String toString() {
return 'Plant id=$id, name=$name, origin=$origin';
}
}
void main() {
var coffee = Plant(27, 'Coffee', ['Ethiopia', 'Brazil']);
// Create an XML document representing our plant
var document = XmlDocument([
XmlElement(XmlName('plant'), [
XmlAttribute(XmlName('id'), coffee.id.toString()),
XmlElement(XmlName('name'), [], [XmlText(coffee.name)]),
...coffee.origin.map((o) => XmlElement(XmlName('origin'), [], [XmlText(o)]))
])
]);
// Emit XML representing our plant; using a StringBuffer for output
var buffer = StringBuffer();
document.writePrettyTo(buffer, indent: ' ');
print(buffer.toString());
// To add a generic XML header to the output, prepend it explicitly
print('<?xml version="1.0" encoding="UTF-8"?>\n${buffer.toString()}');
// Use XmlDocument.parse to parse a string of XML into a document structure.
// If the XML is malformed or cannot be mapped onto Plant, a descriptive error
// will be thrown.
var parsedDocument = XmlDocument.parse(buffer.toString());
var parsedPlant = Plant(
int.parse(parsedDocument.rootElement.getAttribute('id')!),
parsedDocument.findAllElements('name').first.text,
parsedDocument.findAllElements('origin').map((e) => e.text).toList(),
);
print(parsedPlant);
var tomato = Plant(81, 'Tomato', ['Mexico', 'California']);
// The nesting structure demonstrates how to create nested XML elements
var nesting = XmlDocument([
XmlElement(XmlName('nesting'), [
XmlElement(XmlName('parent'), [
XmlElement(XmlName('child'), [
XmlElement(XmlName('plant'), [
XmlAttribute(XmlName('id'), coffee.id.toString()),
XmlElement(XmlName('name'), [], [XmlText(coffee.name)]),
...coffee.origin.map((o) => XmlElement(XmlName('origin'), [], [XmlText(o)]))
]),
XmlElement(XmlName('plant'), [
XmlAttribute(XmlName('id'), tomato.id.toString()),
XmlElement(XmlName('name'), [], [XmlText(tomato.name)]),
...tomato.origin.map((o) => XmlElement(XmlName('origin'), [], [XmlText(o)]))
])
])
])
])
]);
buffer.clear();
nesting.writePrettyTo(buffer, indent: ' ');
print(buffer.toString());
}