XML Parsing using NSXMLParser in Swift

In this blog, we will create simple application called XMLDemo, which describes how to parse XML data with Swift Programming Language. XML stands for Extensible Markup Language.  XML File is one simple looks like text file with extension .xml which contains data. In our example, we take XML file from below button :

XML File
Get XML file Link

Now, follow below steps for Parsing :

xml file logo
XML File

(1) Create Swift Project : 

Create project with any name and you must select Language Swift with Device iPhone  and in your Project navigation there is one file with name ViewController.swift. . .

(2) Declare Variables in ViewController.swift file :

Now, declare some variables which we want to parse from XML file or Used in oure Project. For variable declare in Swift Programming write below code :

var objNSXMLParser = NSXMLParser()
var objPostArray = NSMutableArray()
var objPostDictionary = NSMutableDictionary()
var objDataString = NSString()
var objDataTitle = NSMutableString()
var objDataDate = NSMutableString()

 (3) Implement NSXMLParser Delegate & import it’s method :

First of all, Implement NSXMLParserDelegate in ViewController.swift file using below code :

import UIKit

class ViewController: UIViewController, NSXMLParserDelegate
{
}

Now, write method of NSXMLParserDelegate methods, which looks like :

func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!)
{

}
    
func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!)
{

}
    
func parser(parser: NSXMLParser!, foundCharacters string: String!)
{
    
}

 (4) Create function startParsing() :

Now, create function named startParsing() and called it from viewDidLoad() method :

func startParsing()
{
     objPostArray = []
     objNSXMLParser = NSXMLParser(contentsOfURL: (NSURL.URLWithString("http://images.apple.com/main/rss/hotnews/hotnews.rss")))
     objNSXMLParser.delegate = self
     objNSXMLParser.parse() 
}

Here, you must set delegate = self for NSXMLParser, if delegate not set than no any NSXMLParserDelegate called automatically. After, objXMLParser.parse() all 3 methods of delegate called step by step.

(5) Retrieve data from XML file using Delegate Method :

Now, all above 3 delegate method called step by step and for retrieve data from file to our local variables which we declares in ViewController.swift file by using below code :

// NSXMLParserDelegate Method
func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!)
{
     objDataString = elementName
     if (elementName as NSString).isEqualToString("item") {
          objPostDictionary = NSMutableDictionary.alloc()
          objPostDictionary = [:]
          objDataTitle = NSMutableString.alloc()
          objDataTitle = ""
          objDataDate = NSMutableString.alloc()
          objDataDate = ""
      }
}
    
func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!)
{
     if (elementName as NSString).isEqualToString("item") {
          if objDataTitle != nil {
               objPostDictionary.setObject(objDataTitle, forKey: "title")
          }
          if objDataDate != nil {
               objPostDictionary.setObject(objDataDate, forKey: "date")
          }
          objPostArray.addObject(objPostDictionary)
      }
}
    
func parser(parser: NSXMLParser!, foundCharacters string: String!)
{
     if objDataString.isEqualToString("title") {
          objDataTitle.appendString(string)
     } else if objDataString.isEqualToString("pubDate") {
          objDataDate.appendString(string)
     }
}

Now, your data are parsed from XML file. you can used whenever you want to use. just Extract from dictionary & array and show on screen… This blog shows only how to parse XML data using NSXMLParser in Swift Programming Language.

i Hart
i Hart


This post has been seen 3871 times.

Team Maffat

News Maffat Com is a Free Guest Blogging Website. Must Try once.

Comments are closed.