xml - 提取响应的编码转换

标签 xml encoding react-native fetch iconv

在 React Native 方法中,我获取了一个以 ISO-8859-1 编码的 xml。

只要提取完成,我就会尝试将其转换为 UTF-8。

这里的代码:

const iconv = require('iconv-lite');

fetch('http://www.band.uol.com.br/rss/colunista_64.xml', {
      headers: {
        "Content-type": "text/xml; charset=ISO-8859-1"
      }
})
.then(res=>res.text()})
.then(text => {
   const decodedText = iconv.decode(Buffer.from(text, 'latin1'), 'latin1')
  , output = iconv.encode(decodedText, 'utf8')
   console.log(output.toString())
})

问题是: body 的所有特殊字符都被替换为“¿½”

对于转换,我使用的是包 iconv-lite
这个问题的更好解决方法是什么?

最佳答案

最好的解决方法是使用 res.arrayBuffer()相反 res.text() , 只要 Buffer 构造函数接受 ArrayBuffer
编码:

fetch('http://www.band.uol.com.br/rss/colunista_64.xml')
      .then(res => res.arrayBuffer())
      .then(arrayBuffer => iconv.decode(new Buffer(arrayBuffer), 'iso-8859-1').toString())
      .then(converted => console.log(converted))

关于xml - 提取响应的编码转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41412253/

相关文章:

javascript - 从 XML 中提取数据不起作用

ANDROID 渲染问题 Null Exception

.net - xml文档中的非法字符

c++ - 如何从文件系统填充 XML 树?

perl - 如何让 Perl 尊重 STDIN/STDOUT/STDERR 的区域设置编码,而不影响文件 IO?

windows - Windows ubuntu 子系统上的 iconv

react-native - 根据旋转/大小更改 Flatlist 列数的脚本问题

java - 使用 MediaRecorder 流屏幕 react native

android - 多行 React-Native Text 组件的宽度不会调整为 Android 上最长文本行的长度

C# - 比较不同编码的字符串