My XML data :
<?xml version="1.0" encoding="utf-8"?>
<feed
xmlns="http://www.w3.org/2005/Atom"
xmlns:idx="urn:atom-extension:indexing">
<entry>
<title type="html">Some Title</title>
<link href="https://www.google.com"></link>
</entry>
</feed>
I want to parse Each <entry>
with its <title>
& <link>
tag so far I have successfully parsed the title but no success with <link>
.
My Code :
type Entry struct {
XMLName xml.Name `xml:"entry"`
Link string `xml:"link"`
Title string `xml:"title"`
}
type Feed struct {
XMLName xml.Name `xml:"feed"`
Entries []Entry `xml:"entry"`
}
func (s Entry) String() string {
return fmt.Sprintf("\t Link : %s - Title : %s
", s.Link, s.Title)
}
Live demo at http://play.golang.org/p/hteQ5RuMco
You're close. Your code doesn't extract the link URL because it is not the value of the element <link>
but it is the value of its attribute "href"
.
You can extract the value of the attribute with the following code:
type Link struct {
Href string `xml:"href,attr"`
}
And your modified Entry
type:
type Entry struct {
XMLName xml.Name `xml:"entry"`
Link Link `xml:"link"`
Title string `xml:"title"`
}
Try your modified app on the Go Playground.