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.