将XML解组为嵌入式结构体在Go中的实现

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

Unmarshall XML to embedded struct in go

问题

我有一个扁平的XML结构,我想将其解组成一个包含一个嵌入部分的结构体。这可能吗?语法是什么,或者我可以编写什么自定义方法?

在这个例子中,我用xml:""标记嵌套的结构体,这个标记会被"encoding/xml"跳过。

type FloatHolder struct {
    Value float32    `xml:"value"`
}

type pv struct {
    XMLName    xml.Name  `xml:"series"`
    Test1 FloatHolder `xml:""`       // 不会填充 :-(
    Test2 FloatHolder `xml:"nested"` // 会填充
}

func main() {
    contents := `<series>
                   <nested>
                     <value>1234</value>
                   </nested>
                   <value>1234</value>
                 </series>`

    m := &pv{}

    err := xml.Unmarshal([]byte(contents), &m)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%f %f\n", m.Test1.Value, m.Test2.Value)
}

输出: "0.000000 1234.000000"

Playground: https://play.golang.org/p/aEdDLFYqL5

谢谢!

英文:

I have a flat XML structure, which I want to unmarshall in to a struct which has one part embedded. Is this possible? What is the syntax, or what custom method can I write?

In this example, I tag the nested struct with a guess: xml:&quot;&quot;, which is skipped over by "encoding/xml".

type FloatHolder struct {
	Value float32    `xml:&quot;value&quot;`
}


type pv struct {
	XMLName    xml.Name  `xml:&quot;series&quot;`
	Test1 FloatHolder `xml:&quot;&quot;`	 // does not populate :-(
	Test2 FloatHolder `xml:&quot;nested&quot;` // populates
}
func main() {
	contents := `&lt;series&gt;
			       &lt;nested&gt;
	                 &lt;value&gt;1234&lt;/value&gt;
                   &lt;/nested&gt;
			       &lt;value&gt;1234&lt;/value&gt;
                 &lt;/series&gt;`

	m := &amp;pv{}

	err := xml.Unmarshal([]byte(contents), &amp;m)
	if err != nil {
		panic(err)
	}
	fmt.Printf(&quot;%f %f\n&quot;, m.Test1.Value, m.Test2.Value)
}

Output: "0.000000 1234.000000"

Playground: https://play.golang.org/p/aEdDLFYqL5

Thanks!

答案1

得分: 1

**编辑:**根据评论互动。

是的,你可以这样做。

XML:

<series>
    <value>123456</value>
</series>

结构定义:

type FloatHolder struct {
    Value float32 `xml:",chardata"`
}

type pv struct {
    XMLName xml.Name    `xml:"series"`
    Test2   FloatHolder `xml:"value"`
}

Go Playground链接:https://play.golang.org/p/9sWQaw0HlS


实际上,根据你的XML,这不是一个嵌套字段,它属于series元素。

将你的结构更新为以下内容:

type pv struct {
    XMLName xml.Name    `xml:"series"`
    Test1   float32     `xml:"value"`
    Test2   FloatHolder `xml:"nested"`
}

Go Playground链接:https://play.golang.org/p/-mWrUMJXxX

英文:

EDIT: After comment interaction.

Yes you can. Let's say

XML:

&lt;series&gt;
    &lt;value&gt;123456&lt;/value&gt;
&lt;/series&gt;

Struct definition:

type FloatHolder struct {
	Value float32 `xml:&quot;,chardata&quot;`
}

type pv struct {
	XMLName xml.Name    `xml:&quot;series&quot;`
	Test2   FloatHolder `xml:&quot;value&quot;`
}

Go Playground link: https://play.golang.org/p/9sWQaw0HlS


Actually it's not a nested field, as per your XML. It belongs to series element.

Update your struct to following:

type pv struct {
	XMLName xml.Name    `xml:&quot;series&quot;`
	Test1   float32     `xml:&quot;value&quot;`
	Test2   FloatHolder `xml:&quot;nested&quot;`
}

Go Playground Link: https://play.golang.org/p/-mWrUMJXxX

huangapple
  • 本文由 发表于 2017年7月7日 07:09:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/44960183.html
匿名

发表评论

匿名网友

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

确定