This tutorial uses NSXMLParser Object to parse xml files. The parsing results are displayed by Table View. This tutorial is built on iOS 9.3 on Xcode 7.3.1.
Open Xcode and create a new single-window application. The name is IOS9XMLParserTutorial, and the organization name and organization logo are determined by yourself. Select Swift as the language and iPhone as the device.
Remove the View Controller from the Storyboard and drag a Navigation Controller to the empty artboard. This Navigation Controller will automatically carry a Table View Controller. When you delete the initial View Controller the corresponding storyboard starting point is also removed. So we first select the newly added Navigation Controller and tick the "Is Initial View Controller" checkbox in the Attribute Inspector as the starting point of the new storyboard.
property to Subtitle in the Attributes Inspector.
Class Add a new file, name it TableViewController, and set it as a subclass of UITableViewController.
File and add a new file. Name it Books.xml
<?xml version="1.0"?> <catalog> <book id="1"> <title>To Kill a Mockingbird</title> <author>Harper Lee</author> </book> <book id="2"> <title>1984</title> <author>George Orwell</author> </book> <book id="3"> <title>The Lord of the Rings</title> <author>J.R.R Tolkien</author> </book> <book id="4"> <title>The Catcher in the Rye</title> <author>J.D. Salinger</author> </book> <book id="5"> <title>The Great Gatsby</title> <author>F. Scott Fitzgerald</author> </book> </catalog>
Model as different items in an xml file. Let’s call it Book.swift and replace it with the following code
import Foundation class Book { var bookTitle: String = String() var bookAuthor: String = String() }
variables.
var books: [Book] = [] var eName: String = String() var bookTitle = String() var bookAuthor = String()
override func viewDidLoad() { super.viewDidLoad() if let path = NSBundle.mainBundle().URLForResource("books", withExtension: "xml") { if let parser = NSXMLParser(contentsOfURL: path) { parser.delegate = self parser.parse() } } }
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return books.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) let book = books[indexPath.row] cell.textLabel?.text = book.bookTitle cell.detailTextLabel?.text = book.bookAuthor return cell }
array and presented by the Table View. Next, implement the delegate method of NSXMLParser.
// 1 func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) { eName = elementName if elementName == "book" { bookTitle = String() bookAuthor = String() } } // 2 func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) { if elementName == "book" { let book = Book() book.bookTitle = bookTitle book.bookAuthor = bookAuthor books.append(book) } } // 3 func parser(parser: NSXMLParser, foundCharacters string: String) { let data = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) if (!data.isEmpty) { if eName == "title" { bookTitle += data } else if eName == "author" { bookAuthor += data } } }
Build and run the project. You can see the titles and authors of all books in the TableViewController.
The above is the detailed content of Detailed introduction to XML parsing (graphics and text). For more information, please follow other related articles on the PHP Chinese website!