Create a DOM document from a string

84 views
Skip to first unread message

Gonzalo Chumillas

unread,
Aug 30, 2014, 2:55:17 PM8/30/14
to
Hello Everybody,
I can't figure out how to create a DOM document from a string. It is very straightforward in PHP:

// creates a new DOM document from a string
$doc
= new DOMDocument("1.0", "UTF-8");
$doc
->loadXML($string);

But I have no idea how to do this in Dart, using the native functions and classes.

Thanks.

Günter Zöchbauer

unread,
Aug 30, 2014, 4:10:54 PM8/30/14
to
Client or server and for what purpose?

Anders Holmgren

unread,
Aug 30, 2014, 5:46:13 PM8/30/14
to mi...@dartlang.org

Thomas Løcke

unread,
Aug 31, 2014, 11:02:58 AM8/31/14
to mi...@dartlang.org
Hey Gonzalo,

Den lørdag den 30. august 2014 20.55.17 UTC+2 skrev Gonzalo Chumillas:
Hello Everybody,
I can't figure out how to create a DOM document from a string. It is very straightforward in PHP:

// creates a new DOM document from a string
$doc
= new DOMDocument("1.0", "UTF-8");
$doc
->loadXML($string);

But I have no idea how to do this in Dart, using the native functions and classes.


Maybe a DocumentFragment is good enough for your needs?



void main() {
  String html = '''
    <span id="foo">Foo in a span</span>
  ''';
  SpanElement span = new DocumentFragment.html(html).querySelector('#foo');
  print(span.text);
}

Hans - Jürgen Alps

unread,
Aug 31, 2014, 3:33:45 PM8/31/14
to mi...@dartlang.org
Try DomParser.parseFromString

Gonzalo Chumillas

unread,
Sep 1, 2014, 10:08:56 AM9/1/14
to mi...@dartlang.org
Thanks so much. That's what I was looking for.

El domingo, 31 de agosto de 2014 21:33:45 UTC+2, Hans - Jürgen Alps escribió:
Try DomParser.parseFromString
Reply all
Reply to author
Forward
0 new messages