Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use NSXMLParser objects to parse xml files

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use the NSXMLParser object to parse the xml file", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn about "how to use the NSXMLParser object to parse the xml file" this article.

Open Xcode and create a new single window application. The name is IOS9XMLParserTutorial, and the name and logo of the organization are determined by yourself. Swift is selected for language and iPhone is selected for devices.

Remove View Controller from 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 start point is also removed. So let's first select the newly added Navigation Controller and tick the "Is Initial View Controller" check box of Attribute Inspector as the starting point of the new storyboard.

Double-click able View Controller's Title Bar to set it to "Books". Select Table View Cell and set its Style property to Subtitle in Attributes Inspector.

Storyboard looks like this.

Now that we have deleted the initial ViewController, the ViewController.swift can be deleted as well. Select iOS- > Source- > Cocoa Touch Class to add a new file, name it TableViewController, and make it a subclass of UITableViewController.

Go to Storyboard and select TableViewController, and set the Custom Class section to TableViewController in Identity inspector.

Select iOS- > Source- > Swift File to add a new file. Named Books.xml

Open Books.xml and replace it with the following code

To Kill a Mockingbird Harper Lee 1984 George Orwell The Lord of the Rings J.R.R Tolkien The Catcher in the Rye J.D. Salinger The Great Gatsby F. Scott Fitzgerald

Select iOS- > Source- > Swift File to add a new file as the data model for different items in the xml file. We call it Book.swift and replace it with the following code

Import Foundationclass Book {var bookTitle: String = String () var bookAuthor: String = String ()}

Go to the tableViewController.swift file and add the following variables.

Var books: [Book] = [] var eName: String = String () var bookTitle = String () var bookAuthor = String ()

Copy the viewDidLoad method to

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 ()}

The NSXMLParser object parses the books.xml file in bundle. Add the data source and delegate method for the following table View

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}

The title and author data of all books are saved in the books array and rendered by Table View. Next, implement the delegate method of NSXMLParser.

/ 1func 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)}} / / 3func 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}

This method triggers when the parsing object encounters the starting tag of "".

This method triggers when the parsing object encounters the end tag of "".

Here the parsing process is actually performed. The title and author tags will be parsed and the corresponding variables will be initialized.

Build and run the project. The titles and authors of all books can be seen in TableViewController.

The above is all the contents of the article "how to use NSXMLParser objects to parse xml files". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report