1

Previously, I was able to get the contents of the URL with this encoding:

<?xml version = "1.0" encoding = "UTF-8"?>

And parse it into NSString with codes shown below:

NSMutableData *receivedData =  [[[NSMutableData alloc] initWithContentsOfURL:[NSURL URLWithString:authenticateURL]] autorelease];
    NSString *string = [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease];if (string) {   // if connection established, string will look for <error> in XML
        NSRange range = [string rangeOfString:@"<error>"];
        if (range.location == NSNotFound) {
            result = TRUE;
            NSLog(@"Authenticated!");  // if <error> not found, record is present
        }                           //return TRUE to load data
        else {
            result = FALSE;         // <error> found in XML, record is not found
            NSLog(@"Not Authenticated!");
        }
    }

However, the contents I need to fetch now is actually without a type of encoding, example:

<?xml version="1.0" ?><authenticate><auth>Y</auth></authenticate>

Therefore now, as I NSLog the string, it is empty. Have tried searching for other methods but all the examples I found, people were fine with using UTF8StringEncoding. Would be glad for any advice :)!Thanks!

  • You should use the NSXMLParser class – Deepesh Aug 21 '12 at 08:13
  • @elppa Have edited my codes to show what am I actually doing. Hmm, i don't want to do all the parsing I just want to convert the XML to string and in that string i find , which is easier. Therefore, if error is not found within that string, the person would be authenticated. :) –  Aug 21 '12 at 08:21
  • thanks ppl i guess it's just the encoding problem that is stopping me from retrieving the codes –  Aug 23 '12 at 15:58

4 Answers4

1

You could use the open source library TBXML in combination with https://gist.github.com/1922643 to convert the received XML into an NSDictionary. It supports NSData (using NSUTF8StringEncoding) and plain NSStrings.

This NSDictionary contains all the information available in the XML and you could just read it using the objectForKey: function.

Robin van Dijke
  • 752
  • 4
  • 13
1

You should use NSXMLParser or use ready parser http://www.developers-life.com/simple-xml-parser-based-on-nsxmlparser-converter.html

Volodymyr B.
  • 3,369
  • 2
  • 30
  • 48
0

you can user NSXMLParser class

this is a good Tutorial show you hw to user this class

iArezki
  • 1,273
  • 2
  • 17
  • 29
  • I guess a little different from the XML i'm supposed to fetch. In the tutorial, the xml is using , UTF-8 encoding. And for the UTF-8 encoding, i can convert it to NSString no problem. But then as the encoding that i need to deal with now is , I am lost. Thanks for the advice anyway :) –  Aug 21 '12 at 08:54
0

Sorry all, actually the reason why I was unable to retrieve the XML from the URL is because of some SSL problem. The solution I used can be found here: How to use NSURLConnection to connect with SSL for an untrusted cert?

Thank you everyone for helping too^^

Community
  • 1
  • 1