Golang rss xml解析 <atom10:link 覆盖 <link>

huangapple go评论92阅读模式
英文:

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 &lt;link&gt; tag value if it comes after &lt;atom10:link ...&gt; tag, otherwise it returns empty.

How do I get the &lt;link&gt; tag value if it appears before &lt;atom10:link...&gt;? Or, how do I get both?

Xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?xml-stylesheet type=&quot;text/xsl&quot; media=&quot;screen&quot; href=&quot;/~d/styles/rss2enclosuresfull.xsl&quot;?&gt;&lt;?xml-stylesheet type=&quot;text/css&quot; media=&quot;screen&quot; href=&quot;http://feeds.feedburner.com/~d/styles/itemcontent.css&quot;?&gt;&lt;rss xmlns:atom=&quot;http://www.w3.org/2005/Atom&quot; xmlns:itunes=&quot;http://www.itunes.com/dtds/podcast-1.0.dtd&quot; xmlns:media=&quot;http://search.yahoo.com/mrss/&quot; xmlns:feedburner=&quot;http://rssnamespace.org/feedburner/ext/1.0&quot; version=&quot;2.0&quot;&gt;
  &lt;channel&gt;
    &lt;title&gt;The Javascript&lt;/title&gt;
    &lt;link&gt;http://javascript.com&lt;/link&gt;
    &lt;atom10:link xmlns:atom10=&quot;http://www.w3.org/2005/Atom&quot; rel=&quot;self&quot; type=&quot;application/rss+xml&quot; href=&quot;http://feeds.feedburner.com/the-javascript-show&quot; /&gt;
    &lt;pubDate&gt;Fri, 01 Mar 2013 23:16:58 GMT&lt;/pubDate&gt;
    &lt;language&gt;en-us&lt;/language&gt;
    &lt;itunes:subtitle&gt;News and discussion about the latest in Javascript.&lt;/itunes:subtitle&gt;
    &lt;itunes:keywords&gt;javascript,java,news,jquery,prototype,mootools,scriptaculous&lt;/itunes:keywords&gt;
    &lt;itunes:explicit&gt;no&lt;/itunes:explicit&gt;
    
    &lt;media:keywords&gt;javascript,java,news,jquery,prototype,mootools,scriptaculous&lt;/media:keywords&gt;
    &lt;media:category scheme=&quot;http://www.itunes.com/dtds/podcast-1.0.dtd&quot;&gt;Technology/Tech News&lt;/media:category&gt;
    &lt;itunes:category text=&quot;Technology&quot;&gt;
      &lt;itunes:category text=&quot;Tech News&quot; /&gt;
    &lt;/itunes:category&gt;
    &lt;item&gt;
      &lt;title&gt;Flight, JS1K, TodoMVC 1.1&lt;/title&gt;
      &lt;link&gt;http://feedproxy.google.com/~r/javascript/~3/a1b2c3d4/123&lt;/link&gt;
      &lt;guid isPermaLink=&quot;false&quot;&gt;http://javascript.com/episodes/123&lt;/guid&gt;
      &lt;pubDate&gt;Fri, 01 Mar 2013 23:16:58 GMT&lt;/pubDate&gt;
      &lt;enclosure url=&quot;http://javascript.com/download/feed/54.mp3&quot; length=&quot;15445543&quot; type=&quot;audio/mpeg&quot; /&gt;
      &lt;itunes:duration&gt;21:23&lt;/itunes:duration&gt;
      &lt;itunes:keywords&gt;javascript, news, javascriptonrails, javascript, web development&lt;/itunes:keywords&gt;
      &lt;media:content url=&quot;http://javascript.com/download/feed/123.mp3&quot; fileSize=&quot;15445543&quot; type=&quot;audio/mpeg&quot; /&gt;
      &lt;itunes:explicit&gt;no&lt;/itunes:explicit&gt;
      &lt;feedburner:origLink&gt;http://javascript.com/episodes/123&lt;/feedburner:origLink&gt;
  &lt;/item&gt;
&lt;/channel&gt;
&lt;/rss&gt;

Go:

(Takes xml filename as arg)

package main

import (
	&quot;bytes&quot;
	&quot;encoding/xml&quot;
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
	&quot;os&quot;
)

func main() {
	srcName := os.Args[1]
	xml := getXmlFile(srcName)
	rss := parseXml(xml)
	displayRssChannel(rss.Channel)
}

func displayRssChannel(rss *RssChannel) {
	fmt.Println(&quot;Title:&quot;, rss.Title)
	fmt.Println(&quot;Link:&quot;, rss.Link)
	fmt.Println(&quot;Last build date:&quot;, rss.LastBuildDate)
	fmt.Println(&quot;Pub date:&quot;, rss.PubDate)
	fmt.Println(&quot;Language:&quot;, rss.Language)
	fmt.Println(&quot;Description:&quot;, rss.Description)
}

func getXmlFile(filename string) []byte {
	f, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Println(&quot;Unable to read src xml file.&quot;, err)
		os.Exit(0)
	}
	return f
}

func parseXml(x []byte) *RssFeed {
	feed := RssFeed{}
	d := xml.NewDecoder(bytes.NewReader(x))
	err := d.Decode(&amp;feed)
	if err != nil {
		fmt.Println(&quot;Failed decoding xml&quot;)
		os.Exit(0)
	}
	return &amp;feed
}

type RssFeed struct {
	XMLName xml.Name    `xml:&quot;rss&quot;`
	Channel *RssChannel `xml:&quot;channel&quot;`
}

type RssChannel struct {
	XMLName       xml.Name `xml:&quot;channel&quot;`
	Title         string   `xml:&quot;title&quot;`
	Description   string   `xml:&quot;description&quot;`
	Link          string   `xml:&quot;link&quot;`
	Language      string   `xml:&quot;language&quot;`
	PubDate       string   `xml:&quot;pubDate&quot;`
	LastBuildDate string   `xml:&quot;lastBuildDate&quot;`
}

答案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 = &quot;RssDefault&quot;
...
Link string `xml:&quot;RssDefault link&quot;`

Using a Url might be more correct.

huangapple
  • 本文由 发表于 2014年8月16日 02:36:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/25332175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定