英文:
Unmarshalling XML using Go: How to find attributes with the same value?
问题
我正在尝试解析下面的XML,如何找到所有<info>
节点中type="Genres"
的节点,并将它们的值存储在[]Genre
中?
<manga id="4199" type="manga" name="Jinki: Extend" precision="manga">
<info type="Main title" lang="EN">Jinki: Extend</info>
<info type="Genres">action</info>
<info type="Genres">science fiction</info>
<info type="Themes">mecha</info>
<info type="Number of tankoubon">9</info>
<info type="Number of pages">186</info>
</manga>
我希望将这些值存储在类似以下结构的结构体中:
// Manga 结构体
type Manga struct {
WorkID int `xml:"id,attr"`
Name string `xml:"name,attr"`
Precision string `xml:"precision,attr"`
Genres []Genre // 这部分我需要帮助
}
// Genre 结构体
type Genre struct {
Value string
}
我知道这个XML不是理想的,但这是我必须处理的,希望你们能帮助我。
提前感谢。
英文:
I'm having trouble unmarshalling the XML below, how do I find all <info>
nodes with type="Genres"
and store their values inside a []Genre
?
<manga id="4199" type="manga" name="Jinki: Extend" precision="manga">
<info type="Main title" lang="EN">Jinki: Extend</info>
<info type="Genres">action</info>
<info type="Genres">science fiction</info>
<info type="Themes">mecha</info>
<info type="Number of tankoubon">9</info>
<info type="Number of pages">186</info>
</manga>
I'm looking to store the values in structs similar to these:
// Manga struct
type Manga struct {
WorkID int `xml:"id,attr"`
Name string `xml:"name,attr"`
Precision string `xml:"precision,attr"`
Genres []Genre `[this is the part I need help on]`
}
// Genre struct
type Genre struct {
Value string
}
I know the XML is not ideal, but it is what I have to work with, I hope you guys can help me with this.
Thanks in advance.
答案1
得分: 3
由于<manga>
包含了一系列的<info>
元素,因此更合理的做法是使用Info
结构体的列表,而不是尝试将<info>
元素翻译成各种类型。我会定义如下的数据结构:
type Manga struct {
WorkID int `xml:"id,attr"`
Name string `xml:"name,attr"`
Precision string `xml:"precision,attr"`
Info []Info `xml:"info"`
}
type Info struct {
Type string `xml:"type,attr"`
Value string `xml:",chardata"`
}
输出结果(以JSON编码方便查看)如下:
{
"WorkID": 4199,
"Name": "Jinki: Extend",
"Precision": "manga",
"Info": [
{
"Type": "Main title",
"Value": "Jinki: Extend"
},
{
"Type": "Genres",
"Value": "action"
},
{
"Type": "Genres",
"Value": "science fiction"
},
{
"Type": "Themes",
"Value": "mecha"
},
{
"Type": "Number of tankoubon",
"Value": "9"
},
{
"Type": "Number of pages",
"Value": "186"
}
]
}
英文:
Since <manga>
contains a list of <info>
elements, it makes more sense to have a list of Info
structs rather than trying to translate the <info>
elements into various types. I would define the data structures like:
type Manga struct {
WorkID int `xml:"id,attr"`
Name string `xml:"name,attr"`
Precision string `xml:"precision,attr"`
Info []Info `xml:"info"`
}
type Info struct {
Type string `xml:"type,attr"`
Value string `xml:",chardata"`
}
The output (json encoded for convenience) looks like:
{
"WorkID": 4199,
"Name": "Jinki: Extend",
"Precision": "manga",
"Info": [
{
"Type": "Main title",
"Value": "Jinki: Extend"
},
{
"Type": "Genres",
"Value": "action"
},
{
"Type": "Genres",
"Value": "science fiction"
},
{
"Type": "Themes",
"Value": "mecha"
},
{
"Type": "Number of tankoubon",
"Value": "9"
},
{
"Type": "Number of pages",
"Value": "186"
}
]
}
答案2
得分: 0
如果XML文件不是很大,我会写一个实用函数,类似于:
type Manga struct {
WorkID int `xml:"id,attr"`
Name string `xml:"name,attr"`
Precision string `xml:"precision,attr"`
Info []Info `xml:"info"`
}
type Info struct {
Type string `xml:"type,attr"`
Data string `xml:",chardata"`
}
func (m *Manga) Genres() []Info {
var g []Info
for _, v := range m.Info {
if v.Type == "Genres" {
g = append(g, v)
}
}
return g
}
在这里查看它的运行效果:https://play.golang.org/p/bebUwwbSwG
英文:
In case the XML won't not very large, I would just write an utility function like:
type Manga struct {
WorkID int `xml:"id,attr"`
Name string `xml:"name,attr"`
Precision string `xml:"precision,attr"`
Info []Info `xml:"info"`
}
type Info struct {
Type string `xml:"type,attr"`
Data string `xml:",chardata"`
}
func (m *Manga) Genres() []Info {
var g []Info
for _, v := range m.Info {
if v.Type == "Genres" {
g = append(g, v)
}
}
return g
}
See it in action: https://play.golang.org/p/bebUwwbSwG
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论