Reputation: 1321
There is a .xml
file that I want to turn into/read as String
in order to then parse it as XML
.
How can I achieve that in Flutter?
ex. xml file: https://www.cbar.az/currencies/16.03.2020.xml
Upvotes: 1
Views: 524
Reputation: 8947
This Dart xml library can parse such files. There is an example included that downloads and parses IP address metadata, very similar to your example file. The tutorial contains further examples of how to read files:
final file = new File('bookshelf.xml');
final document = XmlDocument.parse(file.readAsStringSync());
Or download data:
final HttpClient httpClient = HttpClient();
final url = Uri.parse('http://ip-api.com/xml/');
final request = await httpClient.getUrl(url);
final response = await request.close();
final stream = response
.transform(utf8.decoder)
.transform(const XmlEventDecoder())
.transform(const XmlNormalizer())
.expand((events) => events)
.forEach((event) => print(event));
Upvotes: 2