Giter Club home page Giter Club logo

Comments (7)

tnc1997 avatar tnc1997 commented on August 24, 2024 1

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.

justkawal avatar justkawal commented on August 24, 2024

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.

justkawal avatar justkawal commented on August 24, 2024

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.

tnc1997 avatar tnc1997 commented on August 24, 2024

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.

justkawal avatar justkawal commented on August 24, 2024

Hi @justkawal, could I get you to test the branch feat/49, as I think that an XmlConverter could resolve this issue?

I'm humbly sorry for the delay in response. Thomas

from dart-xml-serializable.

justkawal avatar justkawal commented on August 24, 2024

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.

justkawal avatar justkawal commented on August 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.