Comments (3)
Hi @0xEquinox, thank you very much for your positive feedback!
I'm not sure why it's not working.
It looks like some of the classes might have the wrong annotation (@XmlElement
). @XmlRootElement
is the annotation that generates buildXmlElement
and toXmlElement
methods. You can compare the outputs below to see that in action:
@annotation.XmlEnum(fieldRename: annotation.FieldRename.pascal)
enum Language {
mandarin,
spanish,
english,
hindi,
bengali,
}
@annotation.XmlRootElement(name: 'title')
@annotation.XmlSerializable()
class Title {
@annotation.XmlAttribute(name: 'lang')
Language? language;
@annotation.XmlText()
String? text;
Title({
this.language,
this.text,
});
factory Title.fromXmlElement(XmlElement element) =>
_$TitleFromXmlElement(element);
@override
String toString() {
return 'Title{language: $language, text: $text}';
}
void buildXmlChildren(
XmlBuilder builder, {
Map<String, String> namespaces = const {},
}) =>
_$TitleBuildXmlChildren(
this,
builder,
namespaces: namespaces,
);
void buildXmlElement(
XmlBuilder builder, {
Map<String, String> namespaces = const {},
}) =>
_$TitleBuildXmlElement(
this,
builder,
namespaces: namespaces,
);
List<XmlAttribute> toXmlAttributes({
Map<String, String?> namespaces = const {},
}) =>
_$TitleToXmlAttributes(
this,
namespaces: namespaces,
);
List<XmlNode> toXmlChildren({
Map<String, String?> namespaces = const {},
}) =>
_$TitleToXmlChildren(
this,
namespaces: namespaces,
);
XmlElement toXmlElement({
Map<String, String?> namespaces = const {},
}) =>
_$TitleToXmlElement(
this,
namespaces: namespaces,
);
}
// **************************************************************************
// XmlEnumGenerator
// **************************************************************************
const $LanguageEnumMap = {
Language.mandarin: 'Mandarin',
Language.spanish: 'Spanish',
Language.english: 'English',
Language.hindi: 'Hindi',
Language.bengali: 'Bengali'
};
// **************************************************************************
// XmlSerializableGenerator
// **************************************************************************
void _$TitleBuildXmlChildren(Title instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
final language = instance.language;
final languageSerialized =
language != null ? $LanguageEnumMap[language]! : null;
if (languageSerialized != null) {
builder.attribute('lang', languageSerialized);
}
final text = instance.text;
final textSerialized = text;
if (textSerialized != null) {
builder.text(textSerialized);
}
}
void _$TitleBuildXmlElement(Title instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
builder.element('title', namespaces: namespaces, nest: () {
instance.buildXmlChildren(builder, namespaces: namespaces);
});
}
Title _$TitleFromXmlElement(XmlElement element) {
final language = element.getAttribute('lang');
final text = element.getText();
return Title(
language: language != null
? $LanguageEnumMap.entries
.singleWhere(
(languageEnumMapEntry) =>
languageEnumMapEntry.value == language,
orElse: () => throw ArgumentError(
'`$language` is not one of the supported values: ${$LanguageEnumMap.values.join(', ')}'))
.key
: null,
text: text);
}
List<XmlAttribute> _$TitleToXmlAttributes(Title instance,
{Map<String, String?> namespaces = const {}}) {
final attributes = <XmlAttribute>[];
final language = instance.language;
final languageSerialized =
language != null ? $LanguageEnumMap[language]! : null;
final languageConstructed = languageSerialized != null
? XmlAttribute(XmlName('lang'), languageSerialized)
: null;
if (languageConstructed != null) {
attributes.add(languageConstructed);
}
return attributes;
}
List<XmlNode> _$TitleToXmlChildren(Title instance,
{Map<String, String?> namespaces = const {}}) {
final children = <XmlNode>[];
final text = instance.text;
final textSerialized = text;
final textConstructed =
textSerialized != null ? XmlText(textSerialized) : null;
if (textConstructed != null) {
children.add(textConstructed);
}
return children;
}
XmlElement _$TitleToXmlElement(Title instance,
{Map<String, String?> namespaces = const {}}) {
return XmlElement(
XmlName('title'),
[
...namespaces.toXmlAttributes(),
...instance.toXmlAttributes(namespaces: namespaces)
],
instance.toXmlChildren(namespaces: namespaces));
}
@annotation.XmlEnum(fieldRename: annotation.FieldRename.pascal)
enum Language {
mandarin,
spanish,
english,
hindi,
bengali,
}
@annotation.XmlSerializable()
class Title {
@annotation.XmlAttribute(name: 'lang')
Language? language;
@annotation.XmlText()
String? text;
Title({
this.language,
this.text,
});
factory Title.fromXmlElement(XmlElement element) =>
_$TitleFromXmlElement(element);
@override
String toString() {
return 'Title{language: $language, text: $text}';
}
void buildXmlChildren(
XmlBuilder builder, {
Map<String, String> namespaces = const {},
}) =>
_$TitleBuildXmlChildren(
this,
builder,
namespaces: namespaces,
);
void buildXmlElement(
XmlBuilder builder, {
Map<String, String> namespaces = const {},
}) =>
_$TitleBuildXmlElement(
this,
builder,
namespaces: namespaces,
);
List<XmlAttribute> toXmlAttributes({
Map<String, String?> namespaces = const {},
}) =>
_$TitleToXmlAttributes(
this,
namespaces: namespaces,
);
List<XmlNode> toXmlChildren({
Map<String, String?> namespaces = const {},
}) =>
_$TitleToXmlChildren(
this,
namespaces: namespaces,
);
XmlElement toXmlElement({
Map<String, String?> namespaces = const {},
}) =>
_$TitleToXmlElement(
this,
namespaces: namespaces,
);
}
// **************************************************************************
// XmlEnumGenerator
// **************************************************************************
const $LanguageEnumMap = {
Language.mandarin: 'Mandarin',
Language.spanish: 'Spanish',
Language.english: 'English',
Language.hindi: 'Hindi',
Language.bengali: 'Bengali'
};
// **************************************************************************
// XmlSerializableGenerator
// **************************************************************************
void _$TitleBuildXmlChildren(Title instance, XmlBuilder builder,
{Map<String, String> namespaces = const {}}) {
final language = instance.language;
final languageSerialized =
language != null ? $LanguageEnumMap[language]! : null;
if (languageSerialized != null) {
builder.attribute('lang', languageSerialized);
}
final text = instance.text;
final textSerialized = text;
if (textSerialized != null) {
builder.text(textSerialized);
}
}
Title _$TitleFromXmlElement(XmlElement element) {
final language = element.getAttribute('lang');
final text = element.getText();
return Title(
language: language != null
? $LanguageEnumMap.entries
.singleWhere(
(languageEnumMapEntry) =>
languageEnumMapEntry.value == language,
orElse: () => throw ArgumentError(
'`$language` is not one of the supported values: ${$LanguageEnumMap.values.join(', ')}'))
.key
: null,
text: text);
}
List<XmlAttribute> _$TitleToXmlAttributes(Title instance,
{Map<String, String?> namespaces = const {}}) {
final attributes = <XmlAttribute>[];
final language = instance.language;
final languageSerialized =
language != null ? $LanguageEnumMap[language]! : null;
final languageConstructed = languageSerialized != null
? XmlAttribute(XmlName('lang'), languageSerialized)
: null;
if (languageConstructed != null) {
attributes.add(languageConstructed);
}
return attributes;
}
List<XmlNode> _$TitleToXmlChildren(Title instance,
{Map<String, String?> namespaces = const {}}) {
final children = <XmlNode>[];
final text = instance.text;
final textSerialized = text;
final textConstructed =
textSerialized != null ? XmlText(textSerialized) : null;
if (textConstructed != null) {
children.add(textConstructed);
}
return children;
}
What should I do to turn my String containing all the XML into Dart objects?
You'll want to parse the string as an XmlDocument
and then pass the rootElement
to the fromXmlElement
method:
from dart-xml-serializable.
Thank you, using the root element annotation did work, though I'm a little confused as to the difference? When should I use XmlRootElement and when do I use the normal XmlElement? Intuitively I think the root element is the outermost one, but I don't think that's quite right. Anyway, it works now, thank you so much! Have a wonderful day.
from dart-xml-serializable.
When should I use XmlRootElement and when do I use the normal XmlElement?
You would typically use @XmlRootElement
on the class that represents the root element (as this annotation generates buildXmlElement
and toXmlElement
methods) and then @XmlElement
on the fields that are within each class.
https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlrootattribute
https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/XmlRootElement.html
Anyway, it works now, thank you so much! Have a wonderful day.
You're welcome, happy to help, you too!
from dart-xml-serializable.
Related Issues (20)
- Only deserialize fields with an annotation in generate from XML element HOT 5
- Enum-Serializtion does not work anymore if split between multiple files HOT 1
- Reduce boilerplate code HOT 1
- Include beginner friendly docs HOT 1
- ignore attributes HOT 2
- How to describe a self closing optional element? HOT 3
- Add support for fields in superclasses HOT 8
- Add support for import prefixes HOT 3
- Asynchronous deserialization with Streams HOT 3
- Dynamically choose names for XML-elements HOT 3
- feat: Add support for XML base64Binary type HOT 3
- Add support for serializing XML CDATA HOT 2
- Add support for custom converters HOT 3
- Upgrade the version constraint of analyzer HOT 1
- Add support for serializing elements using InnerXml HOT 7
- Better deserialization of empty elements / empty strings HOT 7
- Created List of single null class object but xml element is empty HOT 4
- Nullable fields annotated with XmlCDATA results in incorrectly generated source code HOT 1
- How to Write XML Element Names That Have a $ Sign HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dart-xml-serializable.