英文:
Go Parse XML to struct by tag attribute
问题
我正在尝试按属性和值解析以下XML。
<result name="response" numFound="10775" start="0" maxScore="0.59509283">
<doc>
<str name="cui">c0162311</str>
<str name="display_title">Androgenetic alopecia</str>
<str name="source">GHR</str>
<str name="source_url">http://ghr.nlm.nih.gov/condition/androgenetic-alopecia</str>
<float name="score">0.59509283</float>
</doc>
我已经得到了以下结果
type Response struct {
StrDoc []Str `xml:"result>doc"`
}
type Str struct {
Doc []Doc `xml:"str"`
Score []Score `xml:"float"`
}
type Doc struct {
Key string `xml:"name,attr"`
Value string `xml:",chardata"`
}
type Score struct {
Score string `xml:",chardata"`
}
它产生了
"StrDoc": [
{
"Doc": [
{
"Key": "cui",
"Value": "c0162311"
},
{
"Key": "display_title",
"Value": "Androgenetic alopecia"
},
{
"Key": "source",
"Value": "GHR"
},
{
"Key": "source_url",
"Value": "http://ghr.nlm.nih.gov/condition/androgenetic-alopecia"
}
],
"Score": [
{
"Score": "0.59509283"
}
]
},
期望的输出应该是
"Doc": [
{
"cui": "c0162311",
"display_title": "Androgenetic alopecia",
"source": "GHR",
"Value": "GHR",
"source_url": "http://ghr.nlm.nih.gov/",
"Score": "0.59509283"
}
]
我已经尝试了几个小时,但还没有找到解决方法。
英文:
I'm trying to parse the following XML by attribute and value.
<result name="response" numFound="10775" start="0" maxScore="0.59509283">
<doc>
<str name="cui">c0162311</str>
<str name="display_title">Androgenetic alopecia</str>
<str name="source">GHR</str>
<str name="source_url">http://ghr.nlm.nih.gov/condition/androgenetic-alopecia</str>
<float name="score">0.59509283</float>
</doc>
I've come up with the following
type Response struct {
StrDoc []Str `xml:"result>doc"`
}
type Str struct {
Doc []Doc `xml:"str"`
Score []Score `xml:"float"`
}
type Doc struct {
Key string `xml:"name,attr"`
Value string `xml:",chardata"`
}
type Score struct {
Score string `xml:",chardata"`
}
which produces
"StrDoc": [
{
"Doc": [
{
"Key": "cui",
"Value": "c0162311"
},
{
"Key": "display_title",
"Value": "Androgenetic alopecia"
},
{
"Key": "source",
"Value": "GHR"
},
{
"Key": "source_url",
"Value": "http://ghr.nlm.nih.gov/condition/androgenetic-alopecia"
}
],
"Score": [
{
"Score": "0.59509283"
}
]
},
The desired output would be
"Doc": [
{
"cui": "c0162311",
"display_title": "Androgenetic alopecia",
"source": "GHR",
"Value": "GHR",
"source_url": "http://ghr.nlm.nih.gov/",
"Score": "0.59509283"
}
]
I've been trying to achieve this for hours and I haven't found a way yet.
答案1
得分: 3
你可以通过使用自定义的UnmarshalXML方法,将内部XML解组为一个map:
type Result struct {
Doc Doc `xml:"doc"`
}
type Doc struct {
Elems map[string]string
}
func (doc *Doc) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
type entry struct {
Key string `xml:"name,attr"`
Value string `xml:",chardata"`
}
e := entry{}
doc.Elems = map[string]string{}
for err = d.Decode(&e); err == nil; err = d.Decode(&e) {
doc.Elems[e.Key] = e.Value
}
if err != nil && err != io.EOF {
return err
}
return nil
}
Playground: https://play.golang.org/p/87v_vTXpB-。
英文:
You can unmarshal inner XML into a map by using a custom UnmarshalXML method:
type Result struct {
Doc Doc `xml:"doc"`
}
type Doc struct {
Elems map[string]string
}
func (doc *Doc) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
type entry struct {
Key string `xml:"name,attr"`
Value string `xml:",chardata"`
}
e := entry{}
doc.Elems = map[string]string{}
for err = d.Decode(&e); err == nil; err = d.Decode(&e) {
doc.Elems[e.Key] = e.Value
}
if err != nil && err != io.EOF {
return err
}
return nil
}
Playground: https://play.golang.org/p/87v_vTXpB-.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论