英文:
Decode XML string into struct
问题
我有以下的xml:
<DOC>
<SubGroup1>
<Value1>ABC123</Value1>
<Value2>ABC123</Value2>
<Value3>ABC123</Value3>
<Value4>ABC123</Value4>
</SubGroup1>
<SubGroup2>
<TheTag MyTagAttr="ABC123">
<Value1>ABC123</Value1>
<Value2>ABC123</Value2>
<Value3>ABC123</Value3>
<Value4 MyTagAttr="ABC123">ABC123</Value4>
<Value5>ABC123</Value5>
<Value6>ABC123</Value6>
<Value7>ABC123</Value7>
<Value8>ABC123</Value8>
<Value9>ABC123</Value9>
</TheTag>
</SubGroup2>
</DOC>
我需要将其解码为以下结构体:
type TheTag struct {
XMLName xml.Name `xml:"SubGroup2>TheTag"`
Value1 string `xml:"Value1"`
Value2 string `xml:"Value2"`
}
但是我无法正确地将这个子元素解码到结构体中。
我得到以下错误:
error decoding message content: %!w(xml.UnmarshalError=expected element type <SubGroup2>TheTag> but have <DOC>)
我的代码在Go Playground上可用:https://go.dev/play/p/O688qTBARJm
提前感谢!
英文:
I've the following xml:
<DOC>
<SubGroup1>
<Value1>ABC123</Value1>
<Value2>ABC123</Value2>
<Value3>ABC123</Value3>
<Value4>ABC123</Value4>
</SubGroup1>
<SubGroup2>
<TheTag MyTagAttr="ABC123">
<Value1>ABC123</Value1>
<Value2>ABC123</Value2>
<Value3>ABC123</Value3>
<Value4 MyTagAttr="ABC123">ABC123</Value4>
<Value5>ABC123</Value5>
<Value6>ABC123</Value6>
<Value7>ABC123</Value7>
<Value8>ABC123</Value8>
<Value9>ABC123</Value9>
</TheTag>
</SubGroup2>
</DOC>
And I need to decode into this struct:
type TheTag struct {
XMLName xml.Name `xml:"SubGroup2>TheTag"`
Value1 string `xml:"Value1"`
Value2 string `xml:"Value2"`
}
But I'm not able to decode properly this subelement into the struct.
I'm getting the following error:
error decoding message content: %!w(xml.UnmarshalError=expected element type <SubGroup2>TheTag> but have <DOC>)
My code is available here on Go Playgroud: https://go.dev/play/p/O688qTBARJm
Thanks in advance!
答案1
得分: 2
你应该将标签移动到其他位置。
type TheTag struct {
XMLName xml.Name `xml:"DOC"`
Value1 string `xml:"SubGroup2>TheTag>Value1"`
Value2 string `xml:"SubGroup2>TheTag>Value2"`
}
英文:
You should probably move the tags.
type TheTag struct {
XMLName xml.Name `xml:"DOC"`
Value1 string `xml:"SubGroup2>TheTag>Value1"`
Value2 string `xml:"SubGroup2>TheTag>Value2"`
}
答案2
得分: 0
你可以使用库github.com/clbanning/mxj。
xmlBytes := []byte("<DOC>...</DOC>")
xmlStruct, err := mxj.NewMapXml(xmlBytes)
if err != nil {
// 处理错误
}
theTag, err := xmlStruct.ValuesForPath("DOC.SubGroup2.TheTag")
if err != nil {
// "TheTag" 不存在
}
theTagInterface := theTag.(map[string]interface{})
myTagAttr := theTagInterface["-MyTagAttr"].(string)
val4, err := xmlStruct.ValuesForPath("DOC.SubGroup2.TheTag.Value4")
if err != nil {
// "Value4" 不存在
}
theVal4Interface := val4.(map[string]interface{})
myVal4Attr := theVal4Interface["-MyTagAttr"].(string)
英文:
You can use the library github.com/clbanning/mxj
xmlBytes = []byte("<DOC>...</DOC>")
xmlStruct, err := mxj.NewMapXml(xmlBytes)
if err != nil {
// handle error
}
theTag, err := xmlStruct.ValuesForPath("DOC.SubGroup2.TheTag")
if err != nil {
// Do not exist "TheTag"
}
theTagInterface := theTag.(map[string]interface{})
myTagAttr := theTagInterface["-MyTagAttr"].(string)
val4, err := xmlStruct.ValuesForPath("DOC.SubGroup2.TheTag.Value4")
if err != nil {
// Do not exist "Value4"
}
theVal4Interface := val4.(map[string]interface{})
myVal4Attr := theVal4Interface["-MyTagAttr"].(string)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论