How do I make initWithContentsOfURL wait for an xml page that is generated dynamically?

743 views Asked by At

I am trying to send a query as part a the URL to obtain an XML file, and then trying to parse the XML file using NSXMLParser and initWithContentsOfURL. However the parser is not able to parse the file. I tested the parser with the same file, but this time the file was saved on the server (it was not being generated) and it worked just fine, so I know it is not a problem with the parser.

I have come to think that it does not parse it because I need to load the file before I try to parse it, or give the initWithContentsOfURL time to load the contents. So I tried to put those contents in a NSString and a NSData and using a sleep function as well as using a block but that did not work either.

What would be the best way to go about this problem?

Here is some of the code:

NSString *surl = [NSString stringWithFormat:@"http://lxsrv7.oru.edu/~maria_hernandez/query.xml"];
url = [NSURL URLWithString:surl];
NSString *curl = [[NSString alloc] initWithContentsOfURL:url];

NSLog(@"URL: %@", surl);
NSLog(@"URL Content: %@", curl);

SXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:receivedData];

//Other stuff we have tried:
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:surl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLResponse =  nil;
NSError = nil;
receivedData = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &theResponse error: &error];

Let me know if you have more questions or if you wish to see more code. Thanks!

1

There are 1 answers

1
ssmithstone On

have you tried setting a delegate for the NSXMLParse that implements the NSXMLParserDelegate which has events for parsing the document

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html