Skip to content
/ FeedKit Public

An RSS, Atom and JSON feed Parser and Generator written in Swift.

License

Notifications You must be signed in to change notification settings

nmdias/FeedKit

Repository files navigation


FeedKit is a Swift library for Parsing and Generating RSS, Atom, and JSON feeds.

FeedKit v10 ⚠️

FeedKit v10 is currently in beta. It should be stable enough 👀, but if stable enough is not enough, consider using v9 for now. The beta version includes a new parsing engine, features and improvements, and may contain bugs that still need to be ironed out and unit tested.

Features

Usage

The RSSFeed, AtomFeed and JSONFeed structs makes it easy to fetch and parse feeds from a URL. Here's how to use it:

try await RSSFeed(urlString: "https://developer.apple.com/news/rss/news.rss")

Universal Feed Parser

The Feed enum allows you to handle various feed formats, including RSS, Atom, RDF, and JSON feeds. This makes it a versatile solution for parsing any type of feed.

// Initialize and parse a feed
let feed = try await Feed(urlString: "https://example.com/feed")

// Use a switch to handle different feeds explicitly
switch feed {
case let .atom(feed):   // Atom Syndication Format Feed Model
case let .rss(feed):    // Really Simple Syndication Feed Model
case let .json(feed):   // JSON Feed Model
// ...
}

// Or through optional properties
feed.rssFeed // feed.atomFeed, feed.jsonFeed, ...

Feed Generation

To generate XML for a Feed, create an instance of an RSSFeed, AtomFeed or JSONFeed and populate it with the necessary data.

let feed = RSSFeed(
  channel: .init(
    title: "Breaking News",
    link: "http://www.breakingnews.com/",
    description: "Get the latest updates as they happen.",
    // ...
    items: [
      .init(
        title: "Breaking News: All Hearts are Joyful",
        link: "http://breakingnews.com/2025/01/09/joyful-hearts",
        description: "A heartwarming story of unity and celebration."
        // ...
      ),
    ]
  )
)

Then call toXMLString(formatted:) to generate an XML string.

let xmlString = try feed.toXMLString(formatted: true)
Output
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Breaking News</title>
    <link>http://www.breakingnews.com/</link>
    <description>Get the latest updates as they happen.</description>
    <item>
      <title>Breaking News: All Hearts are Joyful</title>
      <link>http://breakingnews.com/2025/01/09/joyful-hearts</link>
      <description>A heartwarming story of unity and celebration.</description>
    </item>
  </channel>
</rss>

Initializers

All Feed types, Feed, RSSFeed, JSON... provide various initializers for flexibility in loading and parsing feed data.

Show

From a URL String:

init(urlString: String) async throws

From a URL, handling both local file URLs and remote URLs:

init(url: URL) async throws

From a local file URL:

init(fileURL url: URL) throws

From a remote URL:

init(remoteURL url: URL) async throws

From an XML or JSON String:

init(string: String) throws

From raw Data:

init(data: Data) throws

These initializers provide a flexible way to load feeds from the most common sources.

Feed Models

The RSS, Atom, and JSON feed models are highly comprehensive, especially when combined with the supported namespaces. Below is just a small preview of what’s available.

Preview

RSS

feed.title
feed.link
feed.description
feed.language
feed.copyright
feed.managingEditor
feed.webMaster
feed.pubDate
feed.lastBuildDate
feed.categories
feed.generator
feed.docs
feed.cloud
feed.rating
feed.ttl
feed.image
feed.textInput
feed.skipHours
feed.skipDays
//...
feed.dublinCore
feed.syndication
feed.iTunes
// ...

let item = feed.items?.first

item?.title
item?.link
item?.description
item?.author
item?.categories
item?.comments
item?.enclosure
item?.guid
item?.pubDate
item?.source
//...
item?.dublinCore
item?.content
item?.iTunes
item?.media
// ...

Atom

feed.title
feed.subtitle
feed.links
feed.updated
feed.authors
feed.contributors
feed.id
feed.generator
feed.icon
feed.logo
feed.rights
// ...

let entry = feed.entries?.first

entry?.title
entry?.summary
entry?.authors
entry?.contributors
entry?.links
entry?.updated
entry?.categories
entry?.id
entry?.content
entry?.published
entry?.source
entry?.rights
// ...

JSON

feed.version
feed.title
feed.homePageURL
feed.feedUrl
feed.description
feed.userComment
feed.nextUrl
feed.icon
feed.favicon
feed.author
feed.expired
feed.hubs
feed.extensions
// ...

let item = feed.items?.first

item?.id
item?.url
item?.externalUrl
item?.title
item?.contentText
item?.contentHtml
item?.summary
item?.image
item?.bannerImage
item?.datePublished
item?.dateModified
item?.author
item?.url
item?.tags
item?.attachments
item?.extensions
// ...

Installation

To add FeedKit to your Xcode project, follow these steps:

  • Open your project in Xcode and go to the "File" menu.
  • Select "Add Package Dependencies…"
  • Enter the "Package URL": https://github.com/nmdias/FeedKit
  • Select "Add Package"

License

FeedKit is released under the MIT license. See LICENSE for details.