英文:
Golang parse XML with nested nodes of the same name?
问题
我需要解析 XML 代码,
<claims>
<claim>
<claim-text>ABC
<claim-text>PQR</claim-text>
<claim-text>Xyz
<claim-text>A</claim-text>
<claim-text>B</claim-text>
<claim-text>C</claim-text>
</claim-text>
</claim-text>
</claim>
<claim>
<claim-text>PPP
<claim-text>ZZZ</claim-text>
<claim-text>MMM</claim-text>
</claim-text>
</claim>
</claims>
如何获取包含所有声明文本的 'claim' 数组?
我尝试了以下代码,但它没有返回 claim-text 中的任何文本。
type Result struct {
Claims []Claim `xml:"claims>claim"`
}
type Claim struct{
ClaimText []string `xml:"claim-text"`
}
任何帮助将不胜感激。
英文:
I need to parse xml code,
<claims>
<claim>
<claim-text>ABC
<claim-text>PQR</claim-text>
<claim-text>Xyz
<claim-text>A</claim-text>
<claim-text>B</claim-text>
<claim-text>C</claim-text>
</claim-text>
</claim-text>
</claim>
<claim>
<claim-text>PPP
<claim-text>ZZZ</claim-text>
<claim-text>MMM</claim-text>
</claim-text>
</claim>
</claims>
How to get array of 'claim' with inside all claim texts?
I was trying this but it does not give whatever text enclosed in claim-text.
type Result struct {
Claims []Claim `xml:"claims>claim"`
}
type Claim struct{
ClaimText []string `xml:"claim-text"`
}
Any help would be appreciated.
答案1
得分: 2
type Result struct {
Claims []Claim `xml:"claim"`
}
type Claim struct {
ClaimText []ClaimText `xml:"claim-text"`
}
type ClaimText struct {
Value string `xml:",chardata"`
ClaimText []ClaimText `xml:"claim-text"`
}
如果你想去掉空白字符,你可以实现unmarshaler接口:
func (t *ClaimText) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type T ClaimText
if err := d.DecodeElement((*T)(t), &start); err != nil {
return err
}
t.Value = strings.TrimSpace(t.Value)
return nil
}
链接:https://play.golang.org/p/uueAiwG84LH
如果你想去掉空白字符,你可以实现unmarshaler接口:
func (t *ClaimText) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type T ClaimText
if err := d.DecodeElement((*T)(t), &start); err != nil {
return err
}
t.Value = strings.TrimSpace(t.Value)
return nil
}
链接:https://play.golang.org/p/2I1meeBm0pu
英文:
type Result struct {
Claims []Claim `xml:"claim"`
}
type Claim struct {
ClaimText []ClaimText `xml:"claim-text"`
}
type ClaimText struct {
Value string `xml:",chardata"`
ClaimText []ClaimText `xml:"claim-text"`
}
https://play.golang.org/p/uueAiwG84LH
If you want to get rid of the white-space, you can implement the unmarshaler interface:
func (t *ClaimText) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type T ClaimText
if err := d.DecodeElement((*T)(t), &start); err != nil {
return err
}
t.Value = strings.TrimSpace(t.Value)
return nil
}
答案2
得分: 1
请看这个生成以下结构的在线工具:
type Claims struct {
XMLName xml.Name `xml:"claims"`
Text string `xml:",chardata"`
Claim []struct {
Text string `xml:",chardata"`
ClaimText struct {
Text string `xml:",chardata"`
ClaimText []struct {
Text string `xml:",chardata"`
ClaimText []string `xml:"claim-text"`
} `xml:"claim-text"`
} `xml:"claim-text"`
} `xml:"claim"`
}
英文:
Take a look at this online tool that generate the following struct:
type Claims struct {
XMLName xml.Name `xml:"claims"`
Text string `xml:",chardata"`
Claim []struct {
Text string `xml:",chardata"`
ClaimText struct {
Text string `xml:",chardata"`
ClaimText []struct {
Text string `xml:",chardata"`
ClaimText []string `xml:"claim-text"`
} `xml:"claim-text"`
} `xml:"claim-text"`
} `xml:"claim"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论