Golang rss xml解析<atom10:link overrides =“”<link =“”> </atom10:link>

时间:2014-08-15 18:36:34

标签: xml go rss

以下Go代码返回<link>标记后的<atom10:link ...>标记值,否则返回空。

如果<link>标记值出现在<atom10: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>

转到:

(将xml文件名设为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 个答案:

答案 0 :(得分:2)

设置解码器的DefaultSpace属性。然后在标记中指定该空格:

d.DefaultSpace = "RssDefault"
...
Link string `xml:"RssDefault link"`

使用网址可能更正确。