1

I am currently trying to parse a rest XML API response from a STANDS4 API...

The XML file would look something like this:

<results>
  <result>
    <quote>
      To be free it is not enough to beat the system, one must beat the system every day.
    </quote>
    <author>Anonymous</author>
  </result>
</results>

What I want to do is be able to print out the XML response and be able to assign the quote and author to a UILabel. What I currently have is the below code (I have my user ID and my token ID):

import UIKit

class ViewController: UIViewController,NSXMLParserDelegate {
    
    var strXMLData:String = ""
    var currentElement:String = ""
    var passData:Bool=false
    var passName:Bool=false
    var parser = NSXMLParser()
    
    
    @IBOutlet weak var lblNameData: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let url:String="http://www.stands4.com/services/v2/quotes.php?uid=youridhere&tokenid=youridherezf&searchtype=AUTHOR&query=Albert+Einstein"
        let urlToSend: NSURL = NSURL(string: url)!
        // Parse the XML
        parser = NSXMLParser(contentsOfURL: urlToSend)!
        parser.delegate = self
        
        let success:Bool = parser.parse()
        
        if success {
            print("parse success!")
            
            print(strXMLData)
            
            lblNameData.text=strXMLData
            
        } else {
            print("parse failure!")
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
        currentElement=elementName;
        if(elementName=="id" || elementName=="name" || elementName=="cost" || elementName=="description")
        {
            if(elementName=="name"){
                passName=true;
            }
            passData=true;
        }
    }
    
    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        currentElement="";
        if(elementName=="id" || elementName=="name" || elementName=="cost" || elementName=="description")
        {
            if(elementName=="name"){
                passName=false;
            }
            passData=false;
        }
    }
    
    func parser(parser: NSXMLParser, foundCharacters string: String) {
        if(passName){
            strXMLData=strXMLData+"\n\n"+string
        }
        
        if(passData)
        {
            print(string)
        }
    }
    
    func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError) {
        NSLog("failure error: %@", parseError)
    }
}

I currently get the response that the parse was a success but nothing is printed out. I got the above code from this tutorial: http://ashishkakkad.com/2014/10/xml-parsing-in-swift-language-ios-9-nsxmlparser/

So there is most likely some changes I need to make to work with my current API, but I'm not sure what exactly to do.

Any help is appreciated!

1 Answer 1

1

Your first problem is that your XMLParsing functions never look for the proper element names (assuming your example xml is exactly what you are getting). Your XML parsing is looking for "id", "name", "cost", or "description". Your example XML provides "quote" and "author". You need to debug through the XML parsing functions and get a handle on what they are actually doing before using them. Then you should be good to go.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you sir for your help. following your advice I was able to get the info I needed but I was unable to assign the data to a UILabel. My quote is stored in strXMLData (a string) and I want to store it in a UILabel called lblNameData. However, nothing is showing up when I do lblNamedata.text = strXMLData (i printed out strXMLData to make sure that the info I am getting is the quote and it is so I'm not quite sure what I'm doing wrong)
Make sure you are setting the text to the label in the UI (main) thread. Also, try to set the label text at any other time to anything, just to make sure the label text is actually working and visible on screen.
I am updating the UILabel in the viewdidLoad() function. I tried updating the text to "hello" using the lblNamedata.text = "hello" and it displayed/updated successfully to the screen. However, it still doesn't work with the lblNamedata.text = strXMLData (this statement is in the same position as it was in my original post)
oh and i did try dispatch_async(dispatch_get_main_queue()) { self.lblNameData.text=self.strXMLData } but that did not work either :(

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.