从xml.Marshal()的结果中解组XML

huangapple go评论116阅读模式
英文:

Unmarshalling XML from result of xml.Marshal()

问题

我有一个小的struct,我想使用encoding/xml包进行编组和解组:

  1. type Point struct {
  2. X, Y int
  3. z int // 未导出字段
  4. Names []string
  5. }

当我使用encoding/json包时,编码和解码都正常工作。

但是当我使用encoding/xml包时,只有xml.Marshal()正常工作,xml.Unmarshal()返回一个错误:

  1. invalid character '<' looking for beginning of value

这是我用于XML的代码:

  1. p := Point{1, 2, 3, []string{"Bob", "Alice"}}
  2. data, err := xml.Marshal(p)
  3. if err != nil {
  4. fmt.Println("Error:", err)
  5. }
  6. fmt.Println("XML:", string(data))
  7. var pXml Point
  8. err = json.Unmarshal(data, &pXml)
  9. if err != nil {
  10. fmt.Println("Error:", err)
  11. }
  12. fmt.Println("Unmarshalled XML:", pXml)

为什么会出现这个错误,如何解组xml.Marshal()返回的XML输出?

你可以在Go Playground上尝试完整可运行的应用程序。

应用程序的输出:

  1. Input: {1 2 3 [Bob Alice]}
  2. JSON: {"X":1,"Y":2,"Names":["Bob","Alice"]}
  3. Unmarshalled JSON: {1 2 0 [Bob Alice]}
  4. XML: <Point><X>1</X><Y>2</Y><Names>Bob</Names><Names>Alice</Names></Point>
  5. Error: invalid character '<' looking for beginning of value
  6. Unmarshalled XML: {0 0 0 []}
英文:

I have a small struct which I want to marshal and unmarshal using the encoding/xml package:

  1. type Point struct {
  2. X, Y int
  3. z int // unexported
  4. Names []string
  5. }

The encoding/decoding works fine when I use the encoding/json package.

But when I use the encoding/xml package, only the xml.Marshal() works, the xml.Unmarshal() returns an error:

  1. invalid character &#39;&lt;&#39; looking for beginning of value

This is how I do it for XML:

  1. p := Point{1, 2, 3, []string{&quot;Bob&quot;, &quot;Alice&quot;}}
  2. data, err := xml.Marshal(p)
  3. if err != nil {
  4. fmt.Println(&quot;Error:&quot;, err)
  5. }
  6. fmt.Println(&quot;XML:&quot;, string(data))
  7. var pXml Point
  8. err = json.Unmarshal(data, &amp;pXml)
  9. if err != nil {
  10. fmt.Println(&quot;Error:&quot;, err)
  11. }
  12. fmt.Println(&quot;Unmarshalled XML:&quot;, pXml)

Why do I get this error and how can I unmarshal the XML output returned by xml.Marshal()?

Here is the complete, runnable application on the Go Playground to try out.

Output of the application:

  1. Input: {1 2 3 [Bob Alice]}
  2. JSON: {&quot;X&quot;:1,&quot;Y&quot;:2,&quot;Names&quot;:[&quot;Bob&quot;,&quot;Alice&quot;]}
  3. Unmarshalled JSON: {1 2 0 [Bob Alice]}
  4. XML: &lt;Point&gt;&lt;X&gt;1&lt;/X&gt;&lt;Y&gt;2&lt;/Y&gt;&lt;Names&gt;Bob&lt;/Names&gt;&lt;Names&gt;Alice&lt;/Names&gt;&lt;/Point&gt;
  5. Error: invalid character &#39;&lt;&#39; looking for beginning of value
  6. Unmarshalled XML: {0 0 0 []}

答案1

得分: 3

你正在尝试将 XML 解组为 JSON。首先,你执行以下操作:

  1. data, err := xml.Marshal(p)

然后执行以下操作:

  1. err = json.Unmarshal(data, &pXml)

你代码中的第 46 行应该是:

  1. err = xml.Unmarshal(data, &pXml)
英文:

You are trying to unmarshal XML as if it's JSON. First you do

  1. data, err := xml.Marshal(p)

and then

  1. err = json.Unmarshal(data, &amp;pXml)

Line 46 in your code should be

  1. err = xml.Unmarshal(data, &amp;pXml)

huangapple
  • 本文由 发表于 2015年1月19日 21:29:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/28025769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定