0

I'm new to swift and need a little help.

I will like to get the src value from an img tag that has been converted to a string using

string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

I am retrieving posts from a blog and trying to get the image url from each item, so that I can retrieve the image and display it in an image view.

Here is my code:

class myTableViewController: UITableViewController, NSXMLParserDelegate {

var parser: NSXMLParser = NSXMLParser()
var blogPosts: [BlogPost] = []
var postTitle: String = String()
var eName: String = String()

override func viewDidLoad() {
    super.viewDidLoad()
    let url:NSURL = NSURL(string: "http://myurl.com")!
    parser = NSXMLParser(contentsOfURL: url)!
    parser.delegate = self
    parser.parse()
}

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
    eName = elementName
    if elementName == "item" {
        postTitle = String()
    }
}

func parser(parser: NSXMLParser, foundCharacters string: String) {
    let data = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    if (!data.isEmpty) {
        if eName == "title" {
            postTitle += data
        } else if eName == "content:encoded" {
            //get the img src from data

        }
    }
}

The Blogpost class:

class BlogPost {

var postTitle: String = String()

}

8
  • if it is in xml format, then you should go with xml parser, since predicates and other string functions will fail to fullfill your requirements, if there is any thing nested in xml. Commented Nov 13, 2015 at 11:55
  • I'm using NSXMLParser to get the contents from a url and implementing the NSXMLParserDelegate methods... When I get the contents, its in html tags. I want to get the img src attribute value from the first image in the feed Commented Nov 13, 2015 at 12:01
  • post some related to please. Commented Nov 13, 2015 at 12:11
  • I've added my code to the original post Commented Nov 13, 2015 at 12:34
  • check github.com/tid-kijyun/Kanna Commented Nov 13, 2015 at 12:43

1 Answer 1

2

Since I was able to solve the problem, I can answer my question since it is allowed.

I implemented the Kanna Framework and used doc.at_css("img") to get the image tag, then node["src"]! for the img src value. For more info https://github.com/tid-kijyun/Kanna

Here is my code:

func parser(parser: NSXMLParser, foundCharacters string: String) {
        let data = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
        if (!data.isEmpty) {
            if eName == "content:encoded" {
                if let doc = Kanna.HTML(html: data, encoding: NSUTF8StringEncoding) {
                    if let node = doc.at_css("img") {
                        postImageUrl = node["src"]!
                    }
                }
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.