Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
<genres>
  <genre id="5003" title="Бизнес-книги" type="root">
    <genre id="5049" title="Банковское дело" token="bankovskoe_delo" type="genre"/>
    <genre id="5047" title="Кадровый менеджмент" token="kadrovyj_menedzhment" type="container">
      <genre id="5334" title="Аттестация персонала" token="attestaciya_personala" type="genre"/>
      <genre id="5330" title="Гендерные различия" token="gendernyye_razlichiya" type="genre"/>
      <genre id="5332" title="Конфликты" token="konflikty" type="genre"/>
    </genre>
  </genre>
  <genre id="5013" title="Юмористическая литература" type="root">
    <genre id="5201" title="Анекдоты" token="anekdoty" type="genre"/>
    <genre id="5202" title="Зарубежный юмор" token="zarubezhnyy" type="genre"/>
  </genre>
</genres>

So I have maybe unlimited levels of sub-genres. If a type of object is 'root' or 'container' it contains sub-genres, type 'genre' is a single record (like folders and files).

How can I get the list of all of my objects and parse it to class objects ?

Use the xml package. I like to use a factory constructor that takes an XmlElement. There's no reason to limit yourself to a List<String> for the children; you could have a List<Genre> to build a tree that mirrors your XML. (You could, of course, do that instead if you prefer.)

Notice how the factory constructor iterates through any child XML elements, recursively calling itself. The normal constructor is private, so that only the factory constructor uses it. If a genre has no sub-genres, the subGenres list isEmpty.

import 'package:xml/xml.dart';
var sample = '''<?xml version="1.0" encoding="UTF-8"?>
<genres>
  <genre id="5003" title="Бизнес-книги" type="root">
    <genre id="5049" title="Банковское дело" token="bankovskoe_delo" type="genre"/>
    <genre id="5047" title="Кадровый менеджмент" token="kadrovyj_menedzhment" type="container">
      <genre id="5334" title="Аттестация персонала" token="attestaciya_personala" type="genre"/>
      <genre id="5330" title="Гендерные различия" token="gendernyye_razlichiya" type="genre"/>
      <genre id="5332" title="Конфликты" token="konflikty" type="genre"/>
    </genre>
  </genre>
  <genre id="5013" title="Юмористическая литература" type="root">
    <genre id="5201" title="Анекдоты" token="anekdoty" type="genre"/>
    <genre id="5202" title="Зарубежный юмор" token="zarubezhnyy" type="genre"/>
  </genre>
</genres>
class Genre {
  Genre._(this.id, this.title, this.token, this.type, this.subGenres);
  factory Genre.fromElement(XmlElement genreElement) {
    return Genre._(
      genreElement.getAttribute('id'),
      genreElement.getAttribute('title'),
      genreElement.getAttribute('token'),
      genreElement.getAttribute('type'),
      genreElement
          .findElements('genre')
          .map<Genre>((e) => Genre.fromElement(e))
          .toList(),
  String id;
  String title;
  String token;
  String type;
  List<Genre> subGenres;
void main() {
  var root = XmlDocument.parse(sample).getElement('genres');
  var rootGenres = root
      .findElements('genre')
      .map<Genre>((e) => Genre.fromElement(e))
      .toList();
  print(rootGenres);
                Thank you man, you saved my day. Now I need to realize how to put this stuff to firebase an to show it in ListViews )). I think I'll put all the genres to the top level and address the children genres by ids list in parent genre.
– Igor Karelin
                Aug 10, 2020 at 7:58
                Thank you again. I've almost completely finished my production code w/ your help. How can I accept the answer in stack ? And I have another question on flutter xml package, if you can help it'll be cool - stackoverflow.com/questions/63431246/…
– Igor Karelin
                Aug 15, 2020 at 22:22
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.