Go XML解组示例无法编译。

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

Go XML Unmarshal example doesn't compile

问题

go docs中的Xml示例有问题。有人知道如何使其工作吗?当我编译它时,结果是:

xmlexample.go:34: 无法将“name”(类型为string)用作字段值中的xml.Name类型
xmlexample.go:34: 无法将nil用作字段值中的string类型
xmlexample.go:34: 结构初始化器中的值太少

以下是相关的代码:

package main

import (
    "bytes"
    "xml"
)

type Email struct {
    Where string "attr";
    Addr string;
}

type Result struct {
    XMLName xml.Name "result";
    Name string;
    Phone string;
    Email []Email;
}

var buf = bytes.NewBufferString ( `
<result>
    <email where="home">
        <addr>gre@example.com</addr>
    </email>
    <email where='work'>
        <addr>gre@work.com</addr>
    </email>
    <name>Grace R. Emlin</name>
    <address>123 Main Street</address>
</result>`)


func main() {
    var result = Result{ "name", "phone", nil }
    xml.Unmarshal ( buf , &result )
    println ( result.Name )
}
英文:

The Xml example in the go docs is broken. Does anyone know how to make it work? When I compile it, the result is:

xmlexample.go:34: cannot use &quot;name&quot; (type string) as type xml.Name in field value
xmlexample.go:34: cannot use nil as type string in field value
xmlexample.go:34: too few values in struct initializer

Here is the relevant code:

package main

import (
        &quot;bytes&quot;
        &quot;xml&quot;
)

type Email struct {
        Where string &quot;attr&quot;;
        Addr string;
}

type Result struct {
        XMLName xml.Name &quot;result&quot;;
        Name string;
        Phone string;
        Email []Email;
}

var buf = bytes.NewBufferString ( `
&lt;result&gt;
        &lt;email where=&quot;home&quot;&gt;
                &lt;addr&gt;gre@example.com&lt;/addr&gt;
        &lt;/email&gt;
        &lt;email where=&#39;work&#39;&gt;
                &lt;addr&gt;gre@work.com&lt;/addr&gt;
        &lt;/email&gt;
        &lt;name&gt;Grace R. Emlin&lt;/name&gt;
        &lt;address&gt;123 Main Street&lt;/address&gt;
&lt;/result&gt;`)


func main() {
        var result = Result{ &quot;name&quot;, &quot;phone&quot;, nil }
        xml.Unmarshal ( buf , &amp;result )
        println ( result.Name )
}

答案1

得分: 4

type Result 被定义为:

type Result struct {
    XMLName xml.Name "result"
    Name    string
    Phone   string
    Email   []Email
}

type xml.Name 嵌入在 type Result 中,被定义为:

// Name 代表一个带有命名空间标识符(Space)的 XML 名称(Local)。
// 在 Parser.Token 返回的标记中,Space 标识符以规范 URL 的形式给出,而不是在解析的文档中使用的短前缀。
type Name struct {
    Space, Local string
}

因此,使用类似以下的复合字面量进行初始化:

var result = Result{xml.Name{}, "name", "phone", nil}

var result = Result{xml.Name{"space", "local"}, "name", "phone", nil}

var result = Result{Name: "name", Phone: "phone", Email: nil}
英文:

The type Result is defined as:

type Result struct {
	XMLName xml.Name &quot;result&quot;
	Name    string
	Phone   string
	Email   []Email
}

The type xml.Name, embedded in type Result, is defined as:

// A Name represents an XML name (Local) annotated
// with a name space identifier (Space).
// In tokens returned by Parser.Token, the Space identifier
// is given as a canonical URL, not the short prefix used
// in the document being parsed.
type Name struct {
	Space, Local string
}

Therefore, initialize, using composite literals, using something similar to one of:

var result = Result{xml.Name{}, &quot;name&quot;, &quot;phone&quot;, nil}

var result = Result{xml.Name{&quot;space&quot;, &quot;local&quot;}, &quot;name&quot;, &quot;phone&quot;, nil}

var result = Result{Name: &quot;name&quot;, Phone: &quot;phone&quot;, Email: nil}

答案2

得分: 3

这行代码需要修改为:

var result = Result{ Name: "name", Phone: "phone", Email: nil }

然后它应该按预期工作。我提交了一个修复文档的补丁,巧合的是,不久之后就发布了一个新版本,所以再也不会遇到这个特定的问题了。

英文:

The line

var result = Result{ &quot;name&quot;, &quot;phone&quot;, nil }

needs to become

var result = Result{ Name: &quot;name&quot;, Phone: &quot;phone&quot;, Email: nil }

Then it should work as expected. I submitted a patch to fix the documentation and by coincidence a release occurred soon after, so no one should run into this particular issue again.

答案3

得分: 1

它也可以在您提供xml.Name{}以及其他参数的情况下正常工作,如下所示:

var result = Result{ xml.Name{"", "result"}, "name", "phone", nil }
英文:

It also works if you supply xml.Name{} along with the other arguments, like so:

var result = Result{ xml.Name{&quot;&quot;, &quot;result&quot;}, &quot;name&quot;, &quot;phone&quot;, nil }

答案4

得分: 0

这里

var result Result

起作用。

英文:

Here

var result Result

works.

huangapple
  • 本文由 发表于 2010年1月6日 06:11:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/2009492.html
匿名

发表评论

匿名网友

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

确定