如何提取 Atom/RSS

发布于 2024-08-16 23:29:36 字数 1068 浏览 3 评论 0原文

给定一个 URL,如果它有任何 RSS 节点,那么我将添加到数据库中。

例如:

对于 此 URLrssDoc.SelectNodes("rss/channel/ item").Count 大于零。

但对于atom 网址rssDoc.SelectNodes("rss/channel/item").count 等于零。

如何检查 Atom/RSS URL 是否有节点?我尝试过 rssDoc.SelectNodes("feed/entry").Count ,但计数为零。

Public Shared Function HasRssItems(ByVal url as string) As Boolean
Dim myRequest As WebRequest
Dim myResponse As WebResponse
Try
    myRequest = System.Net.WebRequest.Create(url)
    myRequest.Timeout = 5000
    myResponse = myRequest.GetResponse()

    Dim rssStream As Stream = myResponse.GetResponseStream()
    Dim rssDoc As New XmlDocument()
    rssDoc.Load(rssStream)

    Return rssDoc.SelectNodes("rss/channel/item").Count > 0
Catch ex As Exception
    Return False
Finally
    myResponse.Close()
End Try

结束功能

Given a URL, if it has any RSS nodes, then I am adding to the database.

e.g.:

For this URL, rssDoc.SelectNodes("rss/channel/item").Count is greater than zero.

But for the atom url, rssDoc.SelectNodes("rss/channel/item").count is equal to zero.

How can I check if the Atom/RSS URL has any nodes or not? I have tried for rssDoc.SelectNodes("feed/entry").Count, but is giving me zero count.

Public Shared Function HasRssItems(ByVal url as string) As Boolean
Dim myRequest As WebRequest
Dim myResponse As WebResponse
Try
    myRequest = System.Net.WebRequest.Create(url)
    myRequest.Timeout = 5000
    myResponse = myRequest.GetResponse()

    Dim rssStream As Stream = myResponse.GetResponseStream()
    Dim rssDoc As New XmlDocument()
    rssDoc.Load(rssStream)

    Return rssDoc.SelectNodes("rss/channel/item").Count > 0
Catch ex As Exception
    Return False
Finally
    myResponse.Close()
End Try

End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2024-08-23 23:29:36

这里的主要问题是这一行的 XML“节点路径”:

Return rssDoc.SelectNodes("rss/channel/item").Count > 0

仅对 RSS feed 有效,对 ATOM 提要

我过去解决这个问题的一种方法是使用一个简单的函数将 ATOM 提要转换为 RSS 提要。当然,您可以采用另一种方式,或者根本不转换,但是,转换为单一格式使您能够编写一个“通用”代码块,该代码块将提取您可能感兴趣的提要项目的各种元素(即日期、标题等)

有一篇 ATOM 到 RSS 转换器文章 然而,在提供此类转换的代码项目上,这是在 C# 中。我之前自己手动将其转换为 VB.NET,因此这是 VB.NET 版本:

Private Function AtomToRssConverter(ByVal atomDoc As XmlDocument) As XmlDocument
    Dim xmlDoc As XmlDocument = atomDoc
    Dim xmlNode As XmlNode = Nothing
    Dim mgr As New XmlNamespaceManager(xmlDoc.NameTable)
    mgr.AddNamespace("atom", "http://purl.org/atom/ns#")
    Const rssVersion As String = "2.0"
    Const rssLanguage As String = "en-US"
    Dim rssGenerator As String = "RDFFeedConverter"
    Dim memoryStream As New MemoryStream()
    Dim xmlWriter As New XmlTextWriter(memoryStream, Nothing)
    xmlWriter.Formatting = Formatting.Indented
    Dim feedTitle As String = ""
    Dim feedLink As String = ""
    Dim rssDescription As String = ""

    xmlNode = xmlDoc.SelectSingleNode("//atom:title", mgr)
    If xmlNode Is Nothing Then
          This looks like an ATOM v1.0 format, rather than ATOM v0.3.
        mgr.RemoveNamespace("atom", "http://purl.org/atom/ns#")
        mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom")
    End If

    xmlNode = xmlDoc.SelectSingleNode("//atom:title", mgr)
    If Not xmlNode Is Nothing Then
        feedTitle = xmlNode.InnerText
    End If
    xmlNode = xmlDoc.SelectNodes("//atom:link/@href", mgr)(2)
    If Not xmlNode Is Nothing Then
        feedLink = xmlNode.InnerText
    End If
    xmlNode = xmlDoc.SelectSingleNode("//atom:tagline", mgr)
    If Not xmlNode Is Nothing Then
        rssDescription = xmlNode.InnerText
    End If
    xmlNode = xmlDoc.SelectSingleNode("//atom:subtitle", mgr)
    If Not xmlNode Is Nothing Then
        rssDescription = xmlNode.InnerText
    End If

    xmlWriter.WriteStartElement("rss")
    xmlWriter.WriteAttributeString("version", rssVersion)
    xmlWriter.WriteStartElement("channel")
    xmlWriter.WriteElementString("title", feedTitle)
    xmlWriter.WriteElementString("link", feedLink)
    xmlWriter.WriteElementString("description", rssDescription)
    xmlWriter.WriteElementString("language", rssLanguage)
    xmlWriter.WriteElementString("generator", rssGenerator)
    Dim items As XmlNodeList = xmlDoc.SelectNodes("//atom:entry", mgr)
    If items Is Nothing Then
        Throw New FormatException("Atom feed is not in expected format. ")
    Else
        Dim title As String = [String].Empty
        Dim link As String = [String].Empty
        Dim description As String = [String].Empty
        Dim author As String = [String].Empty
        Dim pubDate As String = [String].Empty
        For i As Integer = 0 To items.Count - 1
            Dim nodTitle As XmlNode = items(i)
            xmlNode = nodTitle.SelectSingleNode("atom:title", mgr)
            If Not xmlNode Is Nothing Then
                title = xmlNode.InnerText
            End If
            Try
                link = items(i).SelectSingleNode("atom:link[@rel= alternate ]", mgr).Attributes("href").InnerText
            Catch ex As Exception
                link = items(i).SelectSingleNode("atom:link", mgr).Attributes("href").InnerText
            End Try
            xmlNode = items(i).SelectSingleNode("atom:content", mgr)
            If Not xmlNode Is Nothing Then
                description = xmlNode.InnerText
            End If
            xmlNode = items(i).SelectSingleNode("//atom:name", mgr)
            If Not xmlNode Is Nothing Then
                author = xmlNode.InnerText
            End If
            xmlNode = items(i).SelectSingleNode("atom:issued", mgr)
            If Not xmlNode Is Nothing Then
                pubDate = xmlNode.InnerText
            End If
            xmlNode = items(i).SelectSingleNode("atom:updated", mgr)
            If Not xmlNode Is Nothing Then
                pubDate = xmlNode.InnerText
            End If
            xmlWriter.WriteStartElement("item")
            xmlWriter.WriteElementString("title", title)
            xmlWriter.WriteElementString("link", link)
            If pubDate.Length < 1 Then
                pubDate = Date.MinValue.ToString()
            End If
            xmlWriter.WriteElementString("pubDate", Convert.ToDateTime(pubDate).ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss G\MT"))
            xmlWriter.WriteElementString("author", author)
            xmlWriter.WriteElementString("description", description)
            xmlWriter.WriteEndElement()
        Next
        xmlWriter.WriteEndElement()
        xmlWriter.Flush()
        xmlWriter.Close()
    End If
    Dim retDoc As New XmlDocument()
    Dim outStr As String = Encoding.UTF8.GetString(memoryStream.ToArray())
    retDoc.LoadXml(outStr)
    Return retDoc
End Function

用法相当简单。只需将您的 ATOM feed 加载到 XmlDocument 对象中并将其传递给此函数,您就会得到一个 RSS 格式的 XmlDocument 对象!

如果您有兴趣,我已将整个RSSReader 类放在pastebin.com 上

Your main problem here is that the XML "node path" on this line:

Return rssDoc.SelectNodes("rss/channel/item").Count > 0

is only valid for RSS feeds, not ATOM feeds.

One way I've got over this in the past is to use a simple function to convert an ATOM feed into an RSS feed. Of course, you could go the other way, or not convert at all, however, converting to a single format enables you to write one "generic" chunk of code that will pull out the various elements of a feed's items that you may be interested in (i.e. date, title etc.)

There is an ATOM to RSS Converter article on Code Project that provides such a conversion, however, that is in C#. I have previously manually converted this to VB.NET myself, so here's the VB.NET version:

Private Function AtomToRssConverter(ByVal atomDoc As XmlDocument) As XmlDocument
    Dim xmlDoc As XmlDocument = atomDoc
    Dim xmlNode As XmlNode = Nothing
    Dim mgr As New XmlNamespaceManager(xmlDoc.NameTable)
    mgr.AddNamespace("atom", "http://purl.org/atom/ns#")
    Const rssVersion As String = "2.0"
    Const rssLanguage As String = "en-US"
    Dim rssGenerator As String = "RDFFeedConverter"
    Dim memoryStream As New MemoryStream()
    Dim xmlWriter As New XmlTextWriter(memoryStream, Nothing)
    xmlWriter.Formatting = Formatting.Indented
    Dim feedTitle As String = ""
    Dim feedLink As String = ""
    Dim rssDescription As String = ""

    xmlNode = xmlDoc.SelectSingleNode("//atom:title", mgr)
    If xmlNode Is Nothing Then
          This looks like an ATOM v1.0 format, rather than ATOM v0.3.
        mgr.RemoveNamespace("atom", "http://purl.org/atom/ns#")
        mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom")
    End If

    xmlNode = xmlDoc.SelectSingleNode("//atom:title", mgr)
    If Not xmlNode Is Nothing Then
        feedTitle = xmlNode.InnerText
    End If
    xmlNode = xmlDoc.SelectNodes("//atom:link/@href", mgr)(2)
    If Not xmlNode Is Nothing Then
        feedLink = xmlNode.InnerText
    End If
    xmlNode = xmlDoc.SelectSingleNode("//atom:tagline", mgr)
    If Not xmlNode Is Nothing Then
        rssDescription = xmlNode.InnerText
    End If
    xmlNode = xmlDoc.SelectSingleNode("//atom:subtitle", mgr)
    If Not xmlNode Is Nothing Then
        rssDescription = xmlNode.InnerText
    End If

    xmlWriter.WriteStartElement("rss")
    xmlWriter.WriteAttributeString("version", rssVersion)
    xmlWriter.WriteStartElement("channel")
    xmlWriter.WriteElementString("title", feedTitle)
    xmlWriter.WriteElementString("link", feedLink)
    xmlWriter.WriteElementString("description", rssDescription)
    xmlWriter.WriteElementString("language", rssLanguage)
    xmlWriter.WriteElementString("generator", rssGenerator)
    Dim items As XmlNodeList = xmlDoc.SelectNodes("//atom:entry", mgr)
    If items Is Nothing Then
        Throw New FormatException("Atom feed is not in expected format. ")
    Else
        Dim title As String = [String].Empty
        Dim link As String = [String].Empty
        Dim description As String = [String].Empty
        Dim author As String = [String].Empty
        Dim pubDate As String = [String].Empty
        For i As Integer = 0 To items.Count - 1
            Dim nodTitle As XmlNode = items(i)
            xmlNode = nodTitle.SelectSingleNode("atom:title", mgr)
            If Not xmlNode Is Nothing Then
                title = xmlNode.InnerText
            End If
            Try
                link = items(i).SelectSingleNode("atom:link[@rel= alternate ]", mgr).Attributes("href").InnerText
            Catch ex As Exception
                link = items(i).SelectSingleNode("atom:link", mgr).Attributes("href").InnerText
            End Try
            xmlNode = items(i).SelectSingleNode("atom:content", mgr)
            If Not xmlNode Is Nothing Then
                description = xmlNode.InnerText
            End If
            xmlNode = items(i).SelectSingleNode("//atom:name", mgr)
            If Not xmlNode Is Nothing Then
                author = xmlNode.InnerText
            End If
            xmlNode = items(i).SelectSingleNode("atom:issued", mgr)
            If Not xmlNode Is Nothing Then
                pubDate = xmlNode.InnerText
            End If
            xmlNode = items(i).SelectSingleNode("atom:updated", mgr)
            If Not xmlNode Is Nothing Then
                pubDate = xmlNode.InnerText
            End If
            xmlWriter.WriteStartElement("item")
            xmlWriter.WriteElementString("title", title)
            xmlWriter.WriteElementString("link", link)
            If pubDate.Length < 1 Then
                pubDate = Date.MinValue.ToString()
            End If
            xmlWriter.WriteElementString("pubDate", Convert.ToDateTime(pubDate).ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss G\MT"))
            xmlWriter.WriteElementString("author", author)
            xmlWriter.WriteElementString("description", description)
            xmlWriter.WriteEndElement()
        Next
        xmlWriter.WriteEndElement()
        xmlWriter.Flush()
        xmlWriter.Close()
    End If
    Dim retDoc As New XmlDocument()
    Dim outStr As String = Encoding.UTF8.GetString(memoryStream.ToArray())
    retDoc.LoadXml(outStr)
    Return retDoc
End Function

Usage is fairly straight forward. Simply load in your ATOM feed into an XmlDocument object and pass it to this function, and you'll get an XmlDocument object back, in RSS format!

If you're interested, I've put an entire RSSReader class up on pastebin.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文