Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Swift Essentials

You're reading from   Swift Essentials Get up and running lightning fast with this practical guide to building applications with Swift

Arrow left icon
Product type Paperback
Published in Dec 2014
Publisher
ISBN-13 9781784396701
Length 228 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Bandlem Limited Bandlem Limited
Author Profile Icon Bandlem Limited
Bandlem Limited
Alex Blewitt Alex Blewitt
Author Profile Icon Alex Blewitt
Alex Blewitt
Arrow right icon
View More author details
Toc

Parsing XML


Although JSON is more commonly used, there are still many XML-based network services. Fortunately, XML parsing has existed in iOS since version 5 in the NSXMLParser class and is simple to access from Swift. For example, some data feeds (such as blog posts) use XML documents such as Atom or RSS.

The NSXMLParser is a stream-oriented parser; that is, it reports individual elements as they are seen. The parser calls the delegate to notify when the elements are seen and have finished. When an element is seen, the parser also includes any attributes that were present; for text nodes, it includes the string content.

Thus, the parsing of an XML file involves some state management in the parser. The example used in this section will parse an Atom (news feed) file, whose (simplified) structure looks as follows:

<feed xmlns="http://www.w3.org/2005/Atom">
  <title>AlBlue's Blog</title>
  <link href="http://alblue.bandlem.com/atom.xml" rel="self"/>
  <entry>
  ...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image