Note that there are some explanatory texts on larger screens.

plurals
  1. PONSXMLParser and parse always returns NO
    text
    copied!<p>After several tutorials I have read, I am trying to create my own class in order to retrieve xml results from PHP requests and use their element in my iOS app.</p> <p>So I have XMLParse.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface XMLParser : NSObject &lt;NSXMLParserDelegate&gt;{ NSString* url; NSXMLParser* parser; NSMutableString* element; } - (id) initXML: (NSString*)_url; @end </code></pre> <p>and the XMLParse.m</p> <pre><code>#import "XMLParser.h" @implementation XMLParser{ NSXMLParser* xmlParser; NSMutableArray* xmlOutputData; NSMutableString* xmlOutputNode; } - (id) initXML:(NSString *)_url{ NSLog(@"initXML\n"); url = _url; xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:url]]; [xmlParser setDelegate:self]; if ([xmlParser parse]) { printf("YES"); } else{ printf("NO"); } return self; } - (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ NSLog(@"Started Element %@", elementName); if ([elementName isEqualToString:@"root"]) { if (!xmlOutputData) { xmlOutputData = [[NSMutableArray alloc] init]; NSString* status = [attributeDict objectForKey:@"success"]; if ([status isEqualToString:@"TRUE"]) { return; } else{ printf("ERROR"); } } } } - (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ [xmlOutputNode appendString:[string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]]; } - (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ if ([elementName isEqualToString:@"username"]) { NSLog(@"%@", xmlOutputNode); } xmlOutputNode = [[NSMutableString alloc]init]; } @end </code></pre> <p>Finally from my MenuView.m I'm initialising an object of XMLParse class</p> <pre><code>XMLParser* newParser = [[XMLParser alloc] initXML:@"http://localhost/conquestOfLancaster/login.php?username=test&amp;password=1234"]; </code></pre> <p>and the output on screen it looks like,</p> <pre><code>2012-12-01 18:58:50.020 CoL[2363:c07] initXML 2012-12-01 18:58:50.030 CoL[2363:1b03] ADDRESPONSE - ADDING TO MEMORY ONLY: http://localhost/conquestOfLancaster/login.php?username=test&amp;password=1234 NO </code></pre> <p>I can't understand what I am missing and always the parse is FALSE. Can anyone help?</p> <p>UPDATE:</p> <p>The problem was that my result from PHP link was not a real XML output (has the structure and that confused me). Is my first try with PHP and that's the reason. I will search how you can generate an XML output from a PHP.</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload