Parse XML in iOS with Swift – Build a RSS News Reader App

[ad_1]
EPISODE DOWNLOAD:
FREE TUTORIAL SERIES:

As a developer, our job is to handle and manipulate data. Data often comes in several popular formats: raw data, JSON and XML.

In this training, let’s talk about parsing XML in Swift since many of you asked. You’ll learn:
+ How to construct XMLParser to parse XML
+ Download XML from a server asynchronously
+ Fetch XML data into Table View and Collection View

EPISODE DOWNLOAD:

*********
ABOUT CODE MASTERY
*********
Code Mastery is hosted by Duc Tran, founder of Developers Academy.

This is his free-style no notes, no teleprompter presentation and live coding broadcast with you guys everyday.

To join Duc’s free courses, register for free at

*********
MEET DUC TRAN
*********

Duc Tran is founder of Developers Academy, one of the world’s leading iOS, Android and Web development trainers.

More than 2,000,000 developers have studied his video trainings; 100,000 developers see his posts each month. Each year, Duc has helped 20,000 plus developers graduate from his online courses or video series.

*********
FREE TRAININGS IN IOS DEVELOPMENT
*********
To subscribe and get free tutorials, courses and weekly content, visit me at:
Connect with Duc on facebook:
Tweet him:
Get daily inspiration:

===========
CODE IN THIS COURSE
===========

class FeedParser: NSObject, XMLParserDelegate
{
// 2
private var rssItems: [RSSItem] = []

private var currentElement = “”
private var currentTitle: String = “” {
didSet {
currentTitle = currentTitle.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}
private var currentDescription: String = “” {
didSet {
currentDescription = currentDescription.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}

private var currentPubDate: String = “” {
didSet {
currentPubDate = currentPubDate.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}

private var parserCompletionHandler: (([RSSItem]) – Void)?

// 3
func parseFeed(url: String, completionHandler: (([RSSItem]) – Void)?) – Void
{
self.parserCompletionHandler = completionHandler

let request = URLRequest(url: URL(string: url)!)
let urlSession = URLSession.shared
let task = urlSession.dataTask(with: request) { (data, response, error) in
guard let data = data else {
if let error = error {
print(error)
}
return
}

// parse xml data
let parser = XMLParser(data: data)
parser.delegate = self
parser.parse()
}

task.resume()
}

// MARK: – XML Parser Delegate

// 4
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:])
{
// we assign the name of the element to currentElement, if the item tag is found, we reset the temporary variables of title, description and pubdate for later use
currentElement = elementName
if currentElement == “item” {
currentTitle = “”
currentDescription = “”
currentPubDate = “”
}
}

// 5 – when the value of an element is found, this method gets called with a string representation of part of the characters of the current element

func parser(_ parser: XMLParser, foundCharacters string: String)
{
switch currentElement {
case “title”: currentTitle += string
case “description” : currentDescription += string
case “pubDate”: currentPubDate += string
default: break
}
}

// 6 – when we reach the closing tag /item is found, this method gets called
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
if elementName == “item” {
let rssItem = RSSItem(title: currentTitle, description: currentDescription, pubDate: currentPubDate)
rssItems += [rssItem]
}
}

parse xml, xml swift, xml iOS, parse xml ios, swift parse xml, swift xml parsing, swift xml parser, iOS rss news reader, swift rss, swift xml parsing tutorial, duc tran, duc tran iOS, developers academy


Posted

in

by

Tags:

Comments

4 responses to “Parse XML in iOS with Swift – Build a RSS News Reader App”

  1. Ngô Anh Duy Avatar

    nếu trong thẻ <description> có thêm các thẻ html như <a> < style=&quot.. thì mình xử lý như nào ?

  2. MrJoeYellow Avatar

    Duc, as always great tutorial!

  3. amar singh Avatar

    you are really doing a good job dude YOU ROCK …

  4. abdou ett Avatar

    Thanks a lot Duc for this great tutorial again! A lot of tricks that make our life easier. Therefore, why did you use OperationQueue rather than DispatchQueue?!!

Leave a Reply

Your email address will not be published. Required fields are marked *