4

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?

1
  • 1
    Your document variable is evidently not a XmlDocument instance. What is responseText? If it s a string containing xml you need to parse it to get an XmlDocument: var document = parse(responseText); Commented Mar 9, 2016 at 4:48

1 Answer 1

10

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'));
}
Sign up to request clarification or add additional context in comments.

4 Comments

I think to start I need to learn to import packages, my problems are these little tricks. How do I do to get that package in my package folder? Or better, where can I get them?
Do step by step what it says in the "Installing" tab on pub.dartlang.org/packages/xml. Also this might be helpful with the general concepts: dartlang.org/tools/pub/get-started.html
Thank you man! It worked! Now I have a little problem I would be thankful for your answer: how do you get an attribute from every node? For example, from the example in the link, imagine that I want to get the language in every node?
storeDocument.findAllElements('book').map((each) => each.getAttribute('title') extracts the titles.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.