Comments (7)
Hi @justkawal, thank you very much for your positive feedback!
This functionality might be able to be covered by the ongoing work in #49 (comment).
In theory implementing the first/third proposal in that comment would allow custom serialization using nested elements.
<?xml version="1.0" encoding="UTF-8"?>
<example>
<a>
<year>2023</year>
<month>10</month>
<day>8</day>
</a>
</example>
@annotation.XmlSerializable()
class Example {
@annotation.XmlElement()
@_CustomDateTimeConverter()
final DateTime a;
...
}
class _CustomDateTimeConverter implements annotation.XmlElementConverter<DateTime> {
const _CustomDateTimeConverter();
@override
DateTime fromXmlElement(XmlElement element) => DateTime(
int.parse(element.getElement('year')!.getText()!),
int.parse(element.getElement('month')!.getText()!),
int.parse(element.getElement('day')!.getText()!),
);
@override
XmlElement toXmlElement(DateTime object) => XmlElement(
XmlName('???'),
[],
[
XmlElement(XmlName('year'), [], [XmlText(object.year.toString())]),
XmlElement(XmlName('month'), [], [XmlText(object.month.toString())]),
XmlElement(XmlName('day'), [], [XmlText(object.day.toString())]),
],
);
}
Out of interest what might the type look like for the title element in your example?
Would it be typed as a string or a class with a nullable random unknown tag field?
from dart-xml-serializable.
Thank you so much for this awesome library.
Much appreciated for all the hard work you did on this awesome tool.
from dart-xml-serializable.
I would be waiting for that feature....
It would be very useful for me....
Any rough expected Idea by when we could have that ready?
from dart-xml-serializable.
Hi @justkawal, could I get you to test the branch feat/49
, as I think that an XmlConverter
could resolve this issue?
from dart-xml-serializable.
Hi @justkawal, could I get you to test the branch
feat/49
, as I think that anXmlConverter
could resolve this issue?
I'm humbly sorry for the delay in response. Thomas
from dart-xml-serializable.
Hey I tried it like this:
On Title class:
@annotation.XmlRootElement(name: 'title')
@annotation.XmlSerializable()
class Title {
@annotation.XmlAttribute(name: 'lang')
String? language;
@InnerXmlConverter() // changed this line
@annotation.XmlElement() // changed this line
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,
);
....
....
....
.... rest of the code same as example from xml_annotation.
Converter
import 'package:xml/xml.dart';
import 'package:xml_annotation/xml_annotation.dart' as annotation;
class InnerXmlConverter implements annotation.XmlConverter<String> {
const InnerXmlConverter();
@override
void buildXmlChildren(
String instance,
XmlBuilder builder, {
Map<String, String> namespaces = const {},
}) {
builder.xml(instance);
}
@override
String fromXmlElement(
XmlElement? element,
) {
return element?.innerXml ?? '';
}
@override
List<XmlAttribute> toXmlAttributes(
String instance, {
Map<String, String?> namespaces = const {},
}) {
return <XmlAttribute>[];
}
@override
List<XmlNode> toXmlChildren(
String instance, {
Map<String, String?> namespaces = const {},
}) {
return <XmlNode>[XmlText(instance)];
}
}
from dart-xml-serializable.
So for clarification simplicity, I just want to set and unset the xml inside the title tag to a variable and thats it, nothing more.
I'm thinking the child tags inside <Title> would be any random tag and I don't want to see what it is and just want to set or unset their xml
with using innerXml
getter from the xml library, on the element object.
<?xml version="1.0"?>
<bookshelf>
<book>
<title lang="English"><AustraliaTag>XML Pocket Reference</AustraliaTag></title>
<author>Simon St. Laurent</author>
<author>Michael James Fitzgerald</author>
<price></price>
</book>
<title lang="English"><EuropeTag>XML Pocket Reference</EuropeTag></title>
<author>Simon St. Laurent</author>
<author>Michael James Fitzgerald</author>
<price></price>
</book>
<book>
<title lang="English">HTML and XHTML Pocket Reference</title>
<author>Jennifer Niederst Robbins</author>
<price></price>
</book>
</bookshelf>
<library>
<room>
<roomnumber><JapanTag>XML Pocket Reference</JapanTag></roomnumber>
</room>
<room>
<roomnumber><AsiaTag >XML Pocket Reference</AsiaTag></roomnumber>
</room>
<room>
<roomnumber><UnknownRandomInnerXml>XML Pocket Reference</UnknownRandomInnerXml></roomnumber>
</room>
</library >
I just want to set <JapanTag>XML Pocket Reference</JapanTag>
to the variable and then unset it as the other value would be <AsiaTag >XML Pocket Reference</AsiaTag>
and I tried using XmlText but it tries to get element.getText() which returns null because it is a element which is random in nature.
So, I know for the title class I can make a Title specific converter and easily do it, but what if I had to do it for the other classes too to set and unset innerXml ?
I would duplicating the same piece of code for each and every class but just renaming the variable.
from dart-xml-serializable.
Related Issues (20)
- 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
- Some methods not generated by builder 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
- 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
- Nullable converter types are not working as intended
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.