1

I am trying to parse simple XML file like this:

<root>
    <subroot>
        <subsubroot>
        test
        </subsubroot>
    </subroot>
</root>

I use following code for this:

#import('dart:core');
#import('dart:io');

class XMLParser {
  parse( File file ) {
    String xml = file.readAsTextSync();
    xml = xml.trim();
    //xml = xml.replaceAll("\n", "");
    print(xml);
    RegExp p = new RegExp("<([^<>]+)>(.*)</\\1>", true);
    Iterable it = p.allMatches(xml);
    print(it);
    Iterator itt = it.iterator();
    while( itt.hasNext() ) {
      print(itt.next().group(0));
    }
  }
}

main() {
  new XMLParser().parse(new File("test.xml"));
}

It looks like it does not work even for multiline parameter set to true. When I remove all newline symbols from the file it is ok.

Am I doing something wrong or is it a time to report a bug :)?

Charles
  • 50,943
  • 13
  • 104
  • 142
firen
  • 1,384
  • 2
  • 14
  • 32
  • 4
    This will not answer your regexp woes directly, but I would recommend reading [this Coding Horror post](http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html) (and please take it as a helpful pointer, not as an attack). – sgmorrison Apr 01 '12 at 21:18
  • I am not sure what should I get from there :) Should I look for Dart html/xml parser to parse xml? – firen Apr 01 '12 at 21:30
  • @firen you should report a bug that Dart needs to add a XML parser. XML documents cannot properly be handled by regular expressions. – Lars Tackmann Apr 01 '12 at 21:39
  • yeah I already did that sometime ago and yes this is not answer for my question and no I am not planning to write full xml parser just simple one for some strings replacement which I will use till dart team will provide their xml parser for VM – firen Apr 01 '12 at 21:44
  • @firen I understand, I have updated the answer with a link to a XML parser written in Dart – Lars Tackmann Apr 22 '12 at 19:53

1 Answers1

1

XML cannot properly be parsed by Regular expressions (see this post for more info) and Dart does currently not ship with a XML parser in its SDK. However there does exists open source Dart XML parser here.

Community
  • 1
  • 1
Lars Tackmann
  • 20,275
  • 13
  • 66
  • 83