Created
August 7, 2019 00:30
-
-
Save hamada147/0ea6716ee99a4f48a28b0273cc5b256f to your computer and use it in GitHub Desktop.
Explaining how to work with XML in iOS with Swift 4.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Book { | |
var bookTitle: String = String() | |
var bookAuthor: String = String() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<catalog> | |
<book> | |
<title>To Kill a Mockingbird</title> | |
<author>Harper Lee</author> | |
</book> | |
<book> | |
<title>1984</title> | |
<author>George Orwell</author> | |
</book> | |
<book> | |
<title>The Lord of the Rings</title> | |
<author>J.R.R Tolkien</author> | |
</book> | |
<book> | |
<title>The Catcher in the Rye</title> | |
<author>J.D. Salinger</author> | |
</book> | |
<book> | |
<title>The Great Gatsby</title> | |
<author>F. Scott Fitzgerald</author> | |
</book> | |
</catalog> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController, XMLParserDelegate { | |
// MARK:- Varibles | |
var books: [Book] = [] | |
// MARK:- Life Cycle | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
if let path = Bundle.main.url(forResource: "books", withExtension: "xml") { | |
if let parser = XMLParser(contentsOf: path) { | |
parser.delegate = self | |
parser.parse() | |
} | |
} | |
} | |
// using this variable to hold the element name in order to know which element I'm currently working on | |
var currentElementName: String = String() | |
// using a variable for each model or value I'm going to parse it so I can add it later to an array or use it in anyway I may see fit | |
var currentBook: Book = Book() | |
// MARK:- XMLParserDelegate | |
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) { | |
self.currentElementName = elementName | |
} | |
/// after finish reading the tag insert the created model in the array (this is just an example) | |
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) { | |
if elementName == "book" { | |
self.books.append(self.currentBook) | |
self.currentBook = Book() | |
} | |
} | |
/// reading tag value | |
func parser(_ parser: XMLParser, foundCharacters string: String) { | |
// removing new lines and un-intended white spaces | |
let data = string.trimmingCharacters(in: .whitespacesAndNewlines) | |
// in case empty don't do anything | |
// otherwise insert the data to the correct location | |
if (!data.isEmpty) { | |
if self.currentElementName == "title" { | |
self.currentBook.bookTitle += data | |
} else if self.currentElementName == "author" { | |
self.currentBook.bookAuthor += data | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment