Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read XML file in Dart

Tags:

xml

dart

I'm new in Dart language.

I'm wanting to read a XML file and I'm following these links (they are practical the same) https://pub.dartlang.org/packages/xml or the github's page https://github.com/renggli/dart-xml.

But I'm having problems with some methods like "findAllElements". I'm importing in my dart file the xml library that they have and importing the collection, core and html:

library xml;    
import 'dart:collection';
import 'dart:core';
import 'dart:html';

My code is:

  var document = responseText;

  var titles = document.findAllElements('trkpt');

  titles
      .map((node) => node.text)
      .forEach(print);

While I test my program I have this error:

NoSuchMethodError: method not found: 'findAllElements'

What do I need to import more? I'm still exploring this language and my goal is just get data from XML and apply a filter for it! Any tutorial? Any suggestion?

like image 977
Blackout Avatar asked Nov 01 '25 19:11

Blackout


1 Answers

I see two problems, but I might be missing parts of your code:

  • You don't specify what responseText is, but it looks to me like a String. As explained in the tutorial you linked, you need to parse the text before you can perform queries on it.

  • You don't import the XML library, please follow the install instructions on https://pub.dartlang.org/packages/xml.

The following minimal example works for me:

library example;

import 'package:xml/xml.dart' as xml;

void main() {
  var storeXml = '<?xml?><store><book title="First"/><book title="Second"/></store>';
  var storeDocument = xml.parse(storeXml);
  print(storeDocument.findAllElements('book'));
}
like image 131
Lukas Renggli Avatar answered Nov 03 '25 21:11

Lukas Renggli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!