英文:
xml parsing in go with repetitive tags
问题
当一个 XML feed 在结构中有多个标签时,我在解析它时遇到了问题:
<feed>
<entry>
:
:
</entry>
<entry>
:
:
</entry>
</feed>
在这种情况下,我通过定义一个 []entries
数组来解析 entry,没有问题。
然而,其中一个 entry 是一个 geocode 标签,它的 valuename 和 value 标签重复出现两次。在这种情况下,如何定义一个结构呢?
<geocode>
<valueName>abc</valueName>
<value>a1</value>
<valueName>def</valueName>
<value>d1</value>
</geocode>
这是我在使用的 Go 程序遇到问题的链接:
https://play.golang.org/p/SE8RXTNbYl
英文:
When an xml feed has multiple tags within a structure I am having issues parsing it:
<feed>
<entry>
:
:
</entry>
<entry>
:
:
</entry>
</feed>
In this case I have no issues parsing the entry by defining an array of []entries
However one of the entries is a geocode tag which has valuename and value tag repeated twice. How does one define a structure in such a case.?
<geocode>
<valueName>abc</valueName>
<value>a1</value>
<valueName>def</valueName>
<value>d1</value>
</geocode>
Here is the go program that I am having issues with
https://play.golang.org/p/SE8RXTNbYl
答案1
得分: 7
如果在同一个父标签下有多个具有相同名称的标签,您可以始终使用一个切片来保存所有出现的标签,无论它们是连续枚举的还是它们之间有其他标签。
为了完整起见,这是您要解析的 XML 片段:
<cap:geocode>
<valueName>FIPS6</valueName>
<value>002090 002290</value>
<valueName>UGC</valueName>
<value>AKZ222</value>
</cap:geocode>
所以只需将您的 geocode
结构从这个:
type geocode struct {
ValueName1 string `xml:"valueName"`
Value1 string `xml:"value"`
ValueName2 string `xml:"valueName"`
Value2 string `xml:"value"`
}
改为这个:
type geocode struct {
ValueNames []string `xml:"valueName"`
Values []string `xml:"value"`
}
并编写代码来打印它们:
gc := v.Entries[0].Geocode
log.Println(len(gc.Values))
log.Println(gc.ValueNames)
log.Println(gc.Values)
for i := range gc.Values {
fmt.Printf("ValueName=%s, Value=%s\n", gc.ValueNames[i], gc.Values[i])
}
输出结果(在 Go Playground 上尝试您修改后的源代码):
2009/11/10 23:00:00 2
2009/11/10 23:00:00 [FIPS6 UGC]
2009/11/10 23:00:00 [002090 002290 AKZ222]
ValueName=FIPS6, Value=002090 002290
ValueName=UGC, Value=AKZ222
英文:
If you have multiple tags with the same name under the same parent tag, you can always use a slice which will hold all the occurrences of the tag, regardless if they are enumerated next to each other or there are other tags between them.
To be complete, this is the XML fragment you want to parse:
<cap:geocode>
<valueName>FIPS6</valueName>
<value>002090 002290</value>
<valueName>UGC</valueName>
<value>AKZ222</value>
</cap:geocode>
So simply change your geocode
struct from this:
type geocode struct {
ValueName1 string `xml:"valueName"`
Value1 string `xml:"value"`
ValueName2 string `xml:"valueName"`
Value2 string `xml:"value"`
}
To this:
type geocode struct {
ValueNames []string `xml:"valueName"`
Values []string `xml:"value"`
}
And code to print them:
gc := v.Entries[0].Geocode
log.Println(len(gc.Values))
log.Println(gc.ValueNames)
log.Println(gc.Values)
for i := range gc.Values {
fmt.Printf("ValueName=%s, Value=%s\n", gc.ValueNames[i], gc.Values[i])
}
Output (try your modified source on the Go Playground):
2009/11/10 23:00:00 2
2009/11/10 23:00:00 [FIPS6 UGC]
2009/11/10 23:00:00 [002090 002290 AKZ222]
ValueName=FIPS6, Value=002090 002290
ValueName=UGC, Value=AKZ222
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论