英文:
Golang rss xml parsing <atom10:link overrides <link>
问题
以下是翻译好的内容:
以下的Go代码在<atom10:link ...>
标签之后返回<link>
标签的值,否则返回空值。
如果我想获取在<atom10:link...>
标签之前出现的<link>
标签的值,该怎么做?或者,我如何获取两者的值?
Xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2enclosuresfull.xsl"?>
<?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
<channel>
<title>The Javascript</title>
<link>http://javascript.com</link>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/the-javascript-show" />
<pubDate>Fri, 01 Mar 2013 23:16:58 GMT</pubDate>
<language>en-us</language>
<itunes:subtitle>News and discussion about the latest in Javascript.</itunes:subtitle>
<itunes:keywords>javascript,java,news,jquery,prototype,mootools,scriptaculous</itunes:keywords>
<itunes:explicit>no</itunes:explicit>
<media:keywords>javascript,java,news,jquery,prototype,mootools,scriptaculous</media:keywords>
<media:category scheme="http://www.itunes.com/dtds/podcast-1.0.dtd">Technology/Tech News</media:category>
<itunes:category text="Technology">
<itunes:category text="Tech News" />
</itunes:category>
<item>
<title>Flight, JS1K, TodoMVC 1.1</title>
<link>http://feedproxy.google.com/~r/javascript/~3/a1b2c3d4/123</link>
<guid isPermaLink="false">http://javascript.com/episodes/123</guid>
<pubDate>Fri, 01 Mar 2013 23:16:58 GMT</pubDate>
<enclosure url="http://javascript.com/download/feed/54.mp3" length="15445543" type="audio/mpeg" />
<itunes:duration>21:23</itunes:duration>
<itunes:keywords>javascript, news, javascriptonrails, javascript, web development</itunes:keywords>
<media:content url="http://javascript.com/download/feed/123.mp3" fileSize="15445543" type="audio/mpeg" />
<itunes:explicit>no</itunes:explicit>
<feedburner:origLink>http://javascript.com/episodes/123</feedburner:origLink>
</item>
</channel>
</rss>
Go:
(以xml文件名作为参数)
package main
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
func main() {
srcName := os.Args[1]
xml := getXmlFile(srcName)
rss := parseXml(xml)
displayRssChannel(rss.Channel)
}
func displayRssChannel(rss *RssChannel) {
fmt.Println("Title:", rss.Title)
fmt.Println("Link:", rss.Link)
fmt.Println("Last build date:", rss.LastBuildDate)
fmt.Println("Pub date:", rss.PubDate)
fmt.Println("Language:", rss.Language)
fmt.Println("Description:", rss.Description)
}
func getXmlFile(filename string) []byte {
f, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("Unable to read src xml file.", err)
os.Exit(0)
}
return f
}
func parseXml(x []byte) *RssFeed {
feed := RssFeed{}
d := xml.NewDecoder(bytes.NewReader(x))
err := d.Decode(&feed)
if err != nil {
fmt.Println("Failed decoding xml")
os.Exit(0)
}
return &feed
}
type RssFeed struct {
XMLName xml.Name `xml:"rss"`
Channel *RssChannel `xml:"channel"`
}
type RssChannel struct {
XMLName xml.Name `xml:"channel"`
Title string `xml:"title"`
Description string `xml:"description"`
Link string `xml:"link"`
Language string `xml:"language"`
PubDate string `xml:"pubDate"`
LastBuildDate string `xml:"lastBuildDate"`
}
英文:
The following Go code returns the <link>
tag value if it comes after <atom10:link ...>
tag, otherwise it returns empty.
How do I get the <link>
tag value if it appears before <atom10:link...>
? Or, how do I get both?
Xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2enclosuresfull.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
<channel>
<title>The Javascript</title>
<link>http://javascript.com</link>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/the-javascript-show" />
<pubDate>Fri, 01 Mar 2013 23:16:58 GMT</pubDate>
<language>en-us</language>
<itunes:subtitle>News and discussion about the latest in Javascript.</itunes:subtitle>
<itunes:keywords>javascript,java,news,jquery,prototype,mootools,scriptaculous</itunes:keywords>
<itunes:explicit>no</itunes:explicit>
<media:keywords>javascript,java,news,jquery,prototype,mootools,scriptaculous</media:keywords>
<media:category scheme="http://www.itunes.com/dtds/podcast-1.0.dtd">Technology/Tech News</media:category>
<itunes:category text="Technology">
<itunes:category text="Tech News" />
</itunes:category>
<item>
<title>Flight, JS1K, TodoMVC 1.1</title>
<link>http://feedproxy.google.com/~r/javascript/~3/a1b2c3d4/123</link>
<guid isPermaLink="false">http://javascript.com/episodes/123</guid>
<pubDate>Fri, 01 Mar 2013 23:16:58 GMT</pubDate>
<enclosure url="http://javascript.com/download/feed/54.mp3" length="15445543" type="audio/mpeg" />
<itunes:duration>21:23</itunes:duration>
<itunes:keywords>javascript, news, javascriptonrails, javascript, web development</itunes:keywords>
<media:content url="http://javascript.com/download/feed/123.mp3" fileSize="15445543" type="audio/mpeg" />
<itunes:explicit>no</itunes:explicit>
<feedburner:origLink>http://javascript.com/episodes/123</feedburner:origLink>
</item>
</channel>
</rss>
Go:
(Takes xml filename as arg)
package main
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
func main() {
srcName := os.Args[1]
xml := getXmlFile(srcName)
rss := parseXml(xml)
displayRssChannel(rss.Channel)
}
func displayRssChannel(rss *RssChannel) {
fmt.Println("Title:", rss.Title)
fmt.Println("Link:", rss.Link)
fmt.Println("Last build date:", rss.LastBuildDate)
fmt.Println("Pub date:", rss.PubDate)
fmt.Println("Language:", rss.Language)
fmt.Println("Description:", rss.Description)
}
func getXmlFile(filename string) []byte {
f, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("Unable to read src xml file.", err)
os.Exit(0)
}
return f
}
func parseXml(x []byte) *RssFeed {
feed := RssFeed{}
d := xml.NewDecoder(bytes.NewReader(x))
err := d.Decode(&feed)
if err != nil {
fmt.Println("Failed decoding xml")
os.Exit(0)
}
return &feed
}
type RssFeed struct {
XMLName xml.Name `xml:"rss"`
Channel *RssChannel `xml:"channel"`
}
type RssChannel struct {
XMLName xml.Name `xml:"channel"`
Title string `xml:"title"`
Description string `xml:"description"`
Link string `xml:"link"`
Language string `xml:"language"`
PubDate string `xml:"pubDate"`
LastBuildDate string `xml:"lastBuildDate"`
}
答案1
得分: 3
将解码器的DefaultSpace属性设置为指定的空间,然后在标签中指定该空间:
d.DefaultSpace = "RssDefault"
...
Link string `xml:"RssDefault link"`
使用URL可能更正确。
英文:
Set the DefaultSpace property of the Decoder. then specify that space in the tag:
d.DefaultSpace = "RssDefault"
...
Link string `xml:"RssDefault link"`
Using a Url might be more correct.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论