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

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

Unmarshalling XML from result of xml.Marshal()

问题

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

type Point struct {
    X, Y  int
    z     int // 未导出字段
    Names []string
}

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

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

invalid character '<' looking for beginning of value

这是我用于XML的代码:

p := Point{1, 2, 3, []string{"Bob", "Alice"}}
data, err := xml.Marshal(p)
if err != nil {
    fmt.Println("Error:", err)
}
fmt.Println("XML:", string(data))

var pXml Point
err = json.Unmarshal(data, &pXml)
if err != nil {
    fmt.Println("Error:", err)
}
fmt.Println("Unmarshalled XML:", pXml)

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

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

应用程序的输出:

Input: {1 2 3 [Bob Alice]}
JSON: {"X":1,"Y":2,"Names":["Bob","Alice"]}
Unmarshalled JSON: {1 2 0 [Bob Alice]}

XML: <Point><X>1</X><Y>2</Y><Names>Bob</Names><Names>Alice</Names></Point>
Error: invalid character '<' looking for beginning of value
Unmarshalled XML: {0 0 0 []}
英文:

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

type Point struct {
	X, Y  int
	z     int // unexported
	Names []string
}

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:

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

This is how I do it for XML:

p := Point{1, 2, 3, []string{&quot;Bob&quot;, &quot;Alice&quot;}}
data, err := xml.Marshal(p)
if err != nil {
	fmt.Println(&quot;Error:&quot;, err)
}
fmt.Println(&quot;XML:&quot;, string(data))

var pXml Point
err = json.Unmarshal(data, &amp;pXml)
if err != nil {
	fmt.Println(&quot;Error:&quot;, err)
}
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:

Input: {1 2 3 [Bob Alice]}
JSON: {&quot;X&quot;:1,&quot;Y&quot;:2,&quot;Names&quot;:[&quot;Bob&quot;,&quot;Alice&quot;]}
Unmarshalled JSON: {1 2 0 [Bob Alice]}

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;
Error: invalid character &#39;&lt;&#39; looking for beginning of value
Unmarshalled XML: {0 0 0 []}

答案1

得分: 3

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

data, err := xml.Marshal(p)

然后执行以下操作:

err = json.Unmarshal(data, &pXml)

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

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

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

data, err := xml.Marshal(p)

and then

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

Line 46 in your code should be

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:

确定