XML unmarshal在第一个元素上不起作用。

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

XML unmarshal not working on First element

问题

我正在尝试解析XML。

type XMLCSFP struct {
	Version string `xml:"version,attr"`
}

type XMLCS struct {
	Container XMLCSFP `xml:"container"`
}

v2 := XMLCS{}
data := `
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
    <rootfiles>
        <rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/>
    </rootfiles>
</container>
`
err = xml.Unmarshal([]byte(data), &v)
if err != nil {
	fmt.Printf("error: %v", err)
	return
}
fmt.Println(v)

它没有显示版本1.0。结构体的值是nil

但是当我用div容器包装XML时,它正常工作。

data := `
<div>
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
    <rootfiles>
        <rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/>
    </rootfiles>
</container>
</div>

第一个示例有什么问题?谢谢!

英文:

I'm trying to unmarshal XML.

type XMLCSFP struct {
	Version string `xml:&quot;version,attr&quot;`
}

type XMLCS struct {
	Container XMLCSFP `xml:&quot;container&quot;`
}


v2 := XMLCS{}
data := `
&lt;container xmlns=&quot;urn:oasis:names:tc:opendocument:xmlns:container&quot; version=&quot;1.0&quot;&gt;
    &lt;rootfiles&gt;
        &lt;rootfile full-path=&quot;EPUB/package.opf&quot; media-type=&quot;application/oebps-package+xml&quot;/&gt;
    &lt;/rootfiles&gt;
&lt;/container&gt;
`
err = xml.Unmarshal([]byte(data), &amp;v)
	if err != nil {
	fmt.Printf(&quot;error: %v&quot;, err)
	return
}
fmt.Println(v)

It's not showing me the version 1.0. The struct value is nil

But when I wrap the xml with div container. It's working fine.

data := `
&lt;div&gt;
&lt;container xmlns=&quot;urn:oasis:names:tc:opendocument:xmlns:container&quot; version=&quot;1.0&quot;&gt;
    &lt;rootfiles&gt;
        &lt;rootfile full-path=&quot;EPUB/package.opf&quot; media-type=&quot;application/oebps-package+xml&quot;/&gt;
    &lt;/rootfiles&gt;
&lt;/container&gt;
&lt;/div&gt;
`

What is the problem with the first one? Thanks!

答案1

得分: 2

XML的根元素将被解组为提供的指针类型。在你的情况下,这是XMLCS。由于version是根元素的属性,如果存在,它将进入XMLCS中名为version的字段。

所以,将你的结构体修改如下可以解决这个问题:

type XMLCS struct {
	XMLName string `xml:"container"`
	Version string `xml:"version,attr"`
}

详细了解XML如何映射到结构体的信息,请阅读Marshal的文档

英文:

The root element of XML is unmarshalled into the type of the pointer provided. In your case, this is XMLCS. Since version is an attribute of the root element, it will go into a field named version in XMLCS, if present.

So changing your struct as below should fix the problem,

type XMLCS struct {
	XMLName string `xml:&quot;container&quot;`
	Version string `xml:&quot;version,attr&quot;`
}

Read the documentation of Marshal for details on how XML is mapped to structs.

huangapple
  • 本文由 发表于 2017年6月13日 16:06:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/44515710.html
匿名

发表评论

匿名网友

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

确定