Golang:解析XML元素值和组的属性 [英] Golang: Parse both XML element value and attributes for groups
本文介绍了Golang:解析XML元素值和组的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< RootLevel status =newtimestamp =1383259529xmlns = http://someplace.com >
< Item active =1status =newitemid =451254>
< SubItem active =1recent =falseusertext =No ideaid =78421>
< SubItemField active =1ready =notype =1> 1.4< / SubItemField>
< SubItemField active =1ready =yestype =2> 4.5< / SubItemField>
< / SubItem>
< / Item>
< / RootLevel>
我不知道如何收集SubItemField元素的值和属性,我的代码是例如:
类型RootLEvel结构{
XMLName xml.Name`xml:RootLevel`
...
Item [] Item
}
type Item struct {
...
SubItem [] SubItem
}
类型SubItem结构{
...
SubItemField [] SubItemField
}
类型SubItemField结构{
活动布尔xml :active,attr`
准备好的字符串`xml:准备好了,attr`
}
我需要属性和值,但获取我知道的值的唯一方法就是使用
SubItemField [] float32
代替我的自定义类型。我使用xml.Unmarshal顺便说一句。
任何想法?
谢谢。
$ b $我想我自己怎么做,我只是在我的结构中添加了一个匹配 xml:的新属性,chardata
所以它看起来像: 类型SubItemField结构{
值float32`xml: ,chardata`
活动bool`xml:活动,attr`
准备好的字符串`xml:准备好了,attr`
}
我希望可以帮助别人,因为没有关于此的文档或示例。
I have an XML structure like this:
<RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com"> <Item active="1" status="new" itemid="451254"> <SubItem active="1" recent="false" usertext="No idea" id="78421"> <SubItemField active="1" ready="no" type="1">1.4</SubItemField> <SubItemField active="1" ready="yes" type="2">4.5</SubItemField> </SubItem> </Item> </RootLevel>
I don't know how I can gather both values and attributes for the SubItemField elements, my code is something like:
type RootLEvel struct { XMLName xml.Name `xml:"RootLevel"` ... Item []Item } type Item struct { ... SubItem []SubItem } type SubItem struct { ... SubItemField []SubItemField } type SubItemField struct { Active bool `xml:"active,attr"` Ready string `xml:"ready,attr"` }
I need both attributes and values but the only way to get values that I know is just use
SubItemField []float32
instead of my custom type. I am using xml.Unmarshal btw.
Any idea?
Thanks.
解决方案I figured myself how to do that, I had simply to add a new property in my struct that match
xml:",chardata"
so it looks like:type SubItemField struct { Value float32 `xml:",chardata"` Active bool `xml:"active,attr"` Ready string `xml:"ready,attr"` }
I hope that can help someone cos there is no documentation or examples about this.
这篇关于Golang:解析XML元素值和组的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文