无法使用值中的空格解组有效内容

时间:2013-06-20 19:44:39

标签: go unmarshalling

我正在使用Golang来读取XML响应。我无法正确读取带空格的值。

这是一个要点:https://gist.github.com/anonymous/5825288

有没有办法让xml.Unmarshal修剪<result>的值,然后将其视为int?

即。

<result>1<result> // no spaces, is marshalled correctly.  The resulting value in the struct is 1

但是

<result> 1 </result>  // with spaces, is marshalled incorrectly as an int.  The resulting value in the struct for result is 0.

1 个答案:

答案 0 :(得分:4)

即使在xml“1”中,这是一个字符串而不是int, 解析器无法将此字符串解析为int。所以0只是默认的int值,

如果您将代码更改为:

err:= xml.Unmarshal([]byte(payload), &mt)
if err != nil {
  fmt.Println(err)
}

您将看到解析期间出错 如果你的xml可以有“1”作为值,我建议在你的struct中使用一个字符串。 或者如果有机会,告诉xml的创建者只使用int而不是字符串,其中int是预期的

相关问题