I have a struct with nested array of struct in format below, I have promoted News struct which is in struct array URL
My RSS Feed is : https://foreignpolicy.com/feed/
Here is the tool I used to generate Go struct from my RSS feed XML to Go struct
type Rss struct {
XMLName xml.Name `xml:"rss"`
Channel struct {
URL []struct {
News
} `xml:"item"`
} `xml:"channel"`
}
type News struct {
Loc string `xml:"link"`
Publishdate string `xml:"pubDate"`
Title string `xml:"title"`
Summary string `xml:"description"`
}
Here is the Goplayround promote News struct
I want to go one step further to promote everything in Channel , so that I can access items in News struct directly from top most Rss struct:
type Rss struct {
XMLName xml.Name `xml:"rss"`
Channel --> seem NOT working ?
}
type Channel struct {
URL []struct {
News
}
}
type News struct {
Loc string `xml:"channel>item>link"` ---> is it the right xml tag ?
Publishdate string `xml:"pubDate"` ---> or this is the right xml tag ?
Title string `xml:"title"`
Summary string `xml:"description"`
}
So I can print it as below :
var URLset Rss
if xmlBytes, err := getXML(url); err != nil {
fmt.Printf("Failed to get XML: %v", err)
} else {
xml.Unmarshal(xmlBytes, &URLset)
}
/************************** XML parser *************************/
for _, URLElement := range **URLset.URL** {
fmt.Println("URLElement:", URLElement)
fmt.Println(
"[Element]:",
"\nTitle #", URLElement.Title,
"\nPublicationDate #", URLElement.Publishdate,
"\nSummary#", URLElement.Summary,
"\nLoc #", URLElement.Loc,
"\n")
}
But it seems print nothing Goplayground Promote Channel