1
我試圖解析像這樣簡單的XML文件多的XML文檔:此飛鏢:正則表達式解析
<root>
<subroot>
<subsubroot>
test
</subsubroot>
</subroot>
</root>
我用下面的代碼:
#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"));
}
它看起來像它即使對於多行參數設置爲true也不行。 當我從文件中刪除所有換行符時,它就沒關係。
我做錯了什麼或者是否該報告錯誤:)?
這不會直接回應你的正則表達式困境,但我會建議閱讀[this Coding Horror post](http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way。 html)(請把它作爲一個有用的指針,而不是攻擊)。 – sgmorrison 2012-04-01 21:18:38
我不知道我應該從那裏得到什麼:)我應該尋找Dart html/xml解析器來解析xml嗎? – firen 2012-04-01 21:30:27
@firen您應該報告Dart需要添加XML解析器的錯誤。 XML文檔無法正確處理正則表達式。 – 2012-04-01 21:39:33