douyue5856 2014-08-15 18:36
浏览 149
已采纳

Golang RSS XML解析<atom10:link覆盖<link>

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条回答 默认 最新

  • dongyanjing5975 2014-08-18 14:02
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧