在Go语言中解码XML的问题

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

Problems decoding XML in go

问题

我有一个关于在Go中解析XML的问题。

我一直在尝试解析这段XML:

<?xml version="1.0" encoding="UTF-8"?>
<response>
    <folders>
        <folder id="78" name="Test 1" />
        <folder id="95" name="Test 2" />
        <folder id="96" name="Test 3" />
    </folders>
</response>

这是我的结构体:

type XmlResponse struct {
    Folders []Folder `xml:"folders>folder"`
}

type Folder struct {
    XMLName xml.Name `xml:"folder"`
    Id      int      `xml:"id,attr"`
    Name    string   `xml:"name,attr"`
}

由于某种原因,Go不会将每个文件夹解析为文件夹数组,如结构体中定义的那样。相反,我得到了以下消息:

"expected element type <folder> but have <folders>"

正如你所看到的,我已经将xml:"folders"添加到文件夹列表中,但它仍然无法正确识别。我尝试将XMLName属性放在不同的位置,但要么出现上述错误,要么只得到一个空的XmlResponse结构体。我还尝试创建一个包含文件夹数组的Folders结构体,结果相同。我是否在XML解码方面有什么概念上的错误?名称可能太接近了吗?

我在Go Playground上制作了一个示例,展示了这个问题:http://play.golang.org/p/XRCGVNzO_O

非常感谢。

英文:

I have a question regarding unmarshalling of XML in Go.

I have been trying to unmarshal this piece of XML

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;response&gt;
    	&lt;folders&gt;
            	&lt;folder id=&quot;78&quot; name=&quot;Test 1&quot; /&gt;
            	&lt;folder id=&quot;95&quot; name=&quot;Test 2&quot; /&gt;
            	&lt;folder id=&quot;96&quot; name=&quot;Test 3&quot; /&gt;
    	&lt;/folders&gt;
&lt;/response&gt;

These are my structs

type XmlResponse struct {
    Folders     []Folder   `xml:&quot;folders&quot;`
}

type Folder struct {
    XMLName    xml.Name `xml:&quot;folder&quot;`
    Id   int    `xml:&quot;id,attr&quot;`
    Name string `xml:&quot;name, attr&quot;`
}

For some reason, Go won't unmarshal each folder into an array of folders, as defined in the struct. Instead i get the message

  &quot;expected element type &lt;folder&gt; but have &lt;folders&gt;&quot;

As you can see, i have added xml:&quot;folders&quot; to the list of folders, but it still won't recognize it correctly. I have tried to place the XMLName attributes in different places but i either end up with the above error, or just an empty XmlResponse struct. I have also tried to make a Folders struct containing an array of Folders, with the same result. Am i conceptually missing something regarding XML decoding? Are the names maybe a little too close to eachother?

I have made an example on Go playground that shows the issue: http://play.golang.org/p/XRCGVNzO_O

Thank you very much.

答案1

得分: 3

你需要将xml:&quot;folders&quot;的元数据更改为xml:&quot;folders&gt;folder&quot;,以匹配folders元素内的folder元素。

请参考这个修改后的playground示例

英文:

You need to change the xml:&quot;folders&quot; meta to xml:&quot;folders&gt;folder&quot; to match the folder elements inside the folders element.

See this modified playground example.

huangapple
  • 本文由 发表于 2014年5月14日 16:54:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/23649926.html
匿名

发表评论

匿名网友

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

确定