I have the following XML which I cannot decode .
<pic:pictures><pic:picture><pic:link rel="extrabig" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/lYcAAOxyeglTbII~/80"/>
<pic:link rel="preview" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/lYcAAOxyeglTbII~/81"/>
<pic:link rel="big" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/lYcAAOxyeglTbII~/79"/>
<pic:link rel="thumb" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/lYcAAOxyeglTbII~/78"/>
<pic:link rel="moreadsthumb" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/lYcAAOxyeglTbII~/77"/></pic:picture>
<pic:picture><pic:link rel="extrabig" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/xl8AAOxyuR5TbIIu/80"/><pic:link rel="preview" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/xl8AAOxyuR5TbIIu/81"/><pic:link rel="big" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/xl8AAOxyuR5TbIIu/79"/><pic:link rel="thumb" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/xl8AAOxyuR5TbIIu/78"/><pic:link rel="moreadsthumb" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/xl8AAOxyuR5TbIIu/77"/></pic:picture>
<pic:picture>
<pic:link rel="extrabig" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/DjEAAOxy9X5TbII2/80"/>
<pic:link rel="preview" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/DjEAAOxy9X5TbII2/81"/>
<pic:link rel="big" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/DjEAAOxy9X5TbII2/79"/>
<pic:link rel="thumb" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/DjEAAOxy9X5TbII2/78"/>
<pic:link rel="moreadsthumb" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/DjEAAOxy9X5TbII2/77"/></pic:picture>
<pic:picture>
<pic:link rel="extrabig" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/HXIAAOxyOalTbIJJ/80"/>
<pic:link rel="preview" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/HXIAAOxyOalTbIJJ/81"/>
<pic:link rel="big" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/HXIAAOxyOalTbIJJ/79"/>
<pic:link rel="thumb" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/HXIAAOxyOalTbIJJ/78"/>
<pic:link rel="moreadsthumb" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/HXIAAOxyOalTbIJJ/77"/></pic:picture>
<pic:picture><pic:link rel="extrabig" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/80AAAMXQeW5TbIJU/80"/>
<pic:link rel="preview" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/80AAAMXQeW5TbIJU/81"/>
<pic:link rel="big" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/80AAAMXQeW5TbIJU/79"/>
<pic:link rel="thumb" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/80AAAMXQeW5TbIJU/78"/>
<pic:link rel="moreadsthumb" href="http://akamai.hd.com/00/s/NzY4WDEwMjQ=/z/80AAAMXQeW5TbIJU/77"/>
</pic:picture></pic:pictures>
I've tried the following struct but it's not working
type Pictures Struct{
Pictures []Picture
}
type Picture struct {
Rel string `xml:"rel,attr"`
Href string `xml:"href,attr"`
}
I see the element name is actually "link" but I don't know how to get that. The following doesn't seem to work either (i.e. I get no result/field)
type Pictures struct{
Pictures []Picture
}
type Link struct {
Link []Link
}
type Link struct {
Rel string `xml:"rel,attr"`
Href string `xml:"href,attr"`
}
You basically have already written your decoder. The only thing that was missing were the element names "link" and "picture" I think. Here is a working example (I have just omitted the variable input
containing your example from above:
package main
import (
"encoding/xml"
"fmt"
"strings"
)
func main() {
type Root struct {
Pictures []struct {
Links []struct {
Rel string `xml:"rel,attr"`
Href string `xml:"href,attr"`
} `xml:"link"`
} `xml:"picture"`
}
var v Root
err := xml.NewDecoder(strings.NewReader(input)).Decode(&v)
if err != nil {
fmt.Println("Error:", err)
return
}
for _, pic := range v.Pictures {
for _, link := range pic.Links {
fmt.Printf("%s: %s
", link.Rel, link.Href)
}
fmt.Println()
}
}