Dart xml parser doesn’t decode utf-8 characters

815

Dart uses UTF-16 for its strings. The xml library has no way of knowing that your data is encoded differently. Use the Utf8Decoder from the dart:convert library to bring your data into the right format before trying to parse it.

import 'dart:convert' show utf8;

http.get(URL).then((response) {
  final utf16Body = utf8.decode(response.bodyBytes);
  final parsed = XmlDocument.parse(utf16Body);
  // ...
});
Share:
815
Naranmandakh Tsogoo
Author by

Naranmandakh Tsogoo

Updated on December 09, 2022

Comments

  • Naranmandakh Tsogoo
    Naranmandakh Tsogoo over 1 year

    I’ve used http get method to get xml response and used xml parser library. But it doesn’t decode utf-8 characters.

    http.get(URL).then((response) {
      final parsed = XmlDocument.parse(response.body);
      // Result is malformed
    });
    
    • Michael Kay
      Michael Kay about 5 years
      I suggest you supply (a) a sample XML document, and (b) an indication of how it is failing: what output or error messages does it produce?