Laurence Wingo
Laurence Wingo

Reputation: 3952

Parse XML properties in Swift

In Swift I'm parsing XML from a vimeo channel I have: https://vimeo.com/channels/1000464/videos/rss

I'm successful in extracting the link, publishing date, and video title. However I'd like to extract the thumbnail which is a url property of the "media:thumbnail" value. Since this value has multiple properties for the height of the thumbnail, width of the thumbnail, and the thumbnail url, I'm unsure of how to enumerate over this value.

Here is the code that does the actual work of parsing the feed to extract the video link and video title:

class VimeoFeedViewController: UIViewController, NSXMLParserDelegate, UITableViewDataSource, UIWebViewDelegate, UITableViewDelegate
{

    @IBOutlet var tbData: UITableView?

    var parser = NSXMLParser()
    var posts = NSMutableArray()
    var elements = NSMutableDictionary()
    var element = NSString()
    var title1 = NSMutableString()
    var date = NSMutableString()
    var link = NSMutableString()
    var webView = UIWebView()
    var boxView = UIView()
    var selectedCell = NSIndexPath()
    var valueToPass:String!
    var viewToPass: UIView!
    var customWebView = UIWebView()
    var url = NSURL()





    //custom code for webviews to show up
    var postTitle: String = String()
    var postLink: String = String()
    var ename: String = String()
    //end of custom code for webviews to show up

    override func viewDidLoad() {
        super.viewDidLoad()
        self.beginParsing()
        self.tbData?.backgroundColor = UIColor(patternImage: UIImage(named: "home-page-background.png")!)
        webView.delegate = self

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func beginParsing()
    {
        posts = []
        parser = NSXMLParser(contentsOfURL:(NSURL(string:"https://vimeo.com/channels/1000464/videos/rss"))!)!
        parser.delegate = self
        parser.parse()
        tbData!.reloadData()

    }

    //XMLParser Methods



    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
    {
        element = elementName
        if (elementName as NSString).isEqualToString("item")
        {
            elements = NSMutableDictionary()
            elements = [:]
            title1 = NSMutableString()
            title1 = ""
            date = NSMutableString()
            date = ""
            link = NSMutableString()
            link = ""
            postTitle = String()
            postLink = String()

        }
    }


    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
    {
        if (elementName as NSString).isEqualToString("item") {
            if !title1.isEqual(nil) {
                elements.setObject(title1, forKey: "title")
            }
            if !date.isEqual(nil) {
                elements.setObject(date, forKey: "date")
            }
            if !link.isEqual(nil) {
                elements.setObject(link, forKey: "link")
            }

            posts.addObject(elements)
        }
    }

    func parser(parser: NSXMLParser, foundCharacters string: String)
    {
        if element.isEqualToString("title") {
            title1.appendString(string)
        }
        if element.isEqualToString("pubDate") {
            date.appendString(string)
        }
        if element.isEqualToString("link") {
            link.appendString(string)
        }
    }

I can also post the code shown from the XML feed but can be pretty lengthy in content. The desired thumbnail value lives in the "media:thumbnail" property of the XML.

Thank you in advance for any help on how to enumerate this xml value that holds multiple properties.

Upvotes: 2

Views: 978

Answers (1)

Sandeep
Sandeep

Reputation: 21144

You should compare the element name in parser:didStartElement:qualifiedName:attributes: and extract attributes from it to get url. It should be something like this. The attributes dict contains all the attributes that are in that element.

For example look at this particular element,

<media:thumbnail height="540" width="960" url="https://i.vimeocdn.com/video/611645334_960.webp"/>

If you read attributes dict for this, it should be like this,

["height": "540", "url": "https://i.vimeocdn.com/video/611645334_960.webp", "width": "960"]

Here is code for extracting url that you want,

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
    if elementName == "media:thumbnail" {
        if let url = attributeDict["url"] {
            print(url)
        }
    }
}

Upvotes: 2

Related Questions