【问题标题】:Go xml.Marshal returns invalid charactersGo xml.Marshal 返回无效字符
【发布时间】:2014-10-17 13:29:47
【问题描述】:

我使用下面的代码生成字符串str的XML编码:

str := string([]byte{0x01})
marshalBytes, _ := xml.Marshal(str)
fmt.Println(string(marshalBytes)) // output: <string>�</string>; � is [239 191 189] in bytes.

显然, � 不等于 0x01。

我该如何解决?

【问题讨论】:

标签: xml encoding go


【解决方案1】:

字节 [239 191 189] 是 Unicode Replacement Character 的 UTF-8 编码。

XML 编组器将字节 0x1 替换为 Unicode 替换字符,因为 字节 0x01 不是 legal character in XML

无法阻止 XML 封送处理程序使用替换。

【讨论】:

  • 如果我想在 xml 文件中传输字节 0x01,我该如何实现?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-19
  • 1970-01-01
  • 2019-02-14
  • 2019-10-24
  • 2013-12-11
相关资源
最近更新 更多