【问题标题】:Dart xml parser doesn’t decode utf-8 charactersDart xml 解析器不解码 utf-8 字符
【发布时间】:2019-02-21 07:29:52
【问题描述】:

我使用httpget 方法来获取xml 响应并使用xml 解析器库。 但它不解码 utf-8 字符。

http.get(URL).then((response) {
  final parsed = XmlDocument.parse(response.body);
  // Result is malformed
});

【问题讨论】:

  • 我建议您提供 (a) 一个示例 XML 文档,以及 (b) 说明它是如何失败的:它会产生什么输出或错误消息?

标签: xml utf-8 dart flutter


【解决方案1】:

Dart 的字符串使用 UTF-16。 xml 库无法知道您的数据编码方式不同。在尝试解析数据之前,使用 dart:convert 库中的 Utf8Decoder 将您的数据转换为正确的格式。

import 'dart:convert' show utf8;

http.get(URL).then((response) {
  final utf16Body = utf8.decode(response.bodyBytes);
  final parsed = XmlDocument.parse(utf16Body);
  // ...
});

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 2021-10-13
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    相关资源
    最近更新 更多