使用Go Unmarshal嵌套xml [英] Unmarshal Nested xml with Go
问题描述
我有下面的代码片段,我一直在试图让它工作的墙上猛撞我的头。我到处寻找解决办法,但没有发现我找到的那些方法。
看起来我的映射关系到 xml.Unmarshal
命令,因为它与嵌套字段有关。下面的代码用于检索名为 unit
的第一个值,并且位于xml代码的顶层。
另外两个字段为零,它们嵌套两层。这意味着结构设置不正确。
package main
import(
encoding / xml
fmt
)
类型datevalue结构{
Date int`xml:date`
值float32`xml:value
}
type pv struct {
XMLName xml.Name`xml:series`
单位字符串`xml:unit`
datevalues datevalue`xml:values> dateValue``
}
func main(){
contents:=`< series>
< timeUnit> DAY< / timeUnit>
< unit> Wh< / unit>< measuredBy> INVERTER< / measuredBy>
<值>< dateValue>
< date> 2015-11-04 00:00:00< / date>
<值> 5935.405< /值>
< / dateValue>< /值>
< / series>`
m:=& pv {}
xml.Unmarshal([] byte(contents),& m)
fmt。 Printf(%s%f%d \ n,m.Unit,m.datevalues.Value,m.datevalues.Date)
}
这里是输出:
Wh 0.000000 0
因为您应该使用导出字段进行编组/解组(请参阅 https: //golang.org/pkg/encoding/xml/ )。
您应该使用
type pv struct {
XMLName xml.Name`xml:series`
单位字符串`xml:unit`
Datevalues datevalue`xml:values> dateValue`
而不是
type pv struct {
XMLName xml.Name`xml:series`
单位字符串`xml:unit`
datevalues datevalue`xml:values> dateValue`
}
查看DateValues字段名称。如果第一个符号是大写的,它将被导出。否则,该字段将被忽略,而 Unmarshal
第二:
之后我发现你忽略了你的错误。请不要忽略它们,它们非常有用。
在 int
/ code>数据类型为 Date
字段数据类型
。 如果您将类型更改为
string
,您的代码将可以正常工作。 第三:
我想你真的想把日期
值解开为时间。时间
。
为此,您可以检查 .org / p / l8qdPXy2KRrel =nofollow noreferrer>去操场
I have the following snippet of code that I have been banging my head on the wall trying to make it work. I have searched everywhere for a solution, but none of the ones that I have found seem to work.
It seems that I have an issue with my mapping for the xml.Unmarshal
command as it pertains to nested fields. The code below works for retrieving the first value which is called unit
, and is on the top level of the xml code.
The other two fields come up as zero, and they are nested two level deep. That implies that the structure isn't set up correctly. Here is the code.
package main
import (
"encoding/xml"
"fmt"
)
type datevalue struct {
Date int `xml:"date"`
Value float32 `xml:"value"`
}
type pv struct {
XMLName xml.Name `xml:"series"`
Unit string `xml:"unit"`
datevalues datevalue `xml:"values>dateValue"`
}
func main() {
contents := `<series>
<timeUnit>DAY</timeUnit>
<unit>Wh</unit><measuredBy>INVERTER</measuredBy>
<values><dateValue>
<date>2015-11-04 00:00:00</date>
<value>5935.405</value>
</dateValue></values>
</series>`
m := &pv{}
xml.Unmarshal([]byte(contents), &m)
fmt.Printf("%s %f %d\n", m.Unit, m.datevalues.Value, m.datevalues.Date)
}
And here is the output:
Wh 0.000000 0
First of all your code doesn't work because you should use exported fields for marshalling/unmarshalling (see https://golang.org/pkg/encoding/xml/).
You should use
type pv struct {
XMLName xml.Name `xml:"series"`
Unit string `xml:"unit"`
Datevalues datevalue `xml:"values>dateValue"`
}
instead of
type pv struct {
XMLName xml.Name `xml:"series"`
Unit string `xml:"unit"`
datevalues datevalue `xml:"values>dateValue"`
}
Look at DateValues field name. If first symbol is uppercased it's will be exported. Otherwise that field will be ignored while Unmarshal
Second:
After it I noticed that you are ignoring your errors. Please don't ignore them, they are critically useful.
Check it out on the go playgroung
As you can see you use int
datatype for Date
field of datatype
.
If you change type to string
, your code will work.
Third:
I think you really want to unmarshall your date
value into time.Time
.
To do so you can check this related question
The complete working code you can try on the go playground
这篇关于使用Go Unmarshal嵌套xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!