我有这样的XML结构:
<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>
Run Code Online (Sandbox Code Playgroud)
我不知道如何收集SubItemField元素的值和属性,我的代码如下:
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"`
}
Run Code Online (Sandbox Code Playgroud)
我需要属性和值,但获取我知道的值的唯一方法就是使用
SubItemField []float32
Run Code Online (Sandbox Code Playgroud)
而不是我的自定义类型.我正在使用xml.Unmarshal btw.
任何的想法?
谢谢.
Dam*_*get 31
我想我自己该怎么做,我只是在我的结构中添加一个匹配的新属性,xml:",chardata"如下所示:
type SubItemField struct {
Value float32 `xml:",chardata"`
Active bool `xml:"active,attr"`
Ready string `xml:"ready,attr"`
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助某人,因为没有关于此的文档或示例.
| 归档时间: |
|
| 查看次数: |
8207 次 |
| 最近记录: |