NSXMLParser parsing failure on reuse but no error code - nsxmlparser

i have 4 NSXMLParser objects.they work fine when i use them for first time but when i use them more than once they fail but return no error code
initialization
-(void)initParsers
{
NSURL* file_url = nil;
FOR(i,4){
file_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"stack%d",0]ofType:#"xml" inDirectory:#"stackXmls"]];
stackXmlParser[i] = [[NSXMLParser alloc] initWithContentsOfURL:file_url];
stackXmlParser[i].delegate = self;
}
}
Parse
BOOL success = [stackXmlParser[currentStack] parse];
if (success)
{
//Do XML stuff
...
} else {
NSLog(#"Error Error Error!!!");
NSError *error = [stackXmlParser[currentStack] parserError];
NSLog(#"Errors with xmlParser: %# %d", [error localizedDescription], error.code);
}
OUTPUT
2012-01-24 16:03:29.542 Cards[5168:207] Error Error Error!!!
2012-01-24 16:03:29.543 Cards[5168:207] Errors with xmlParser: (null) 0

Try using initWithData instead of initWithContentsOfURL
NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathToXMLFile]];
NSLog(#"initWithContentsOfURL 1: %s", ([parser parse])?"OK":"Fail");
NSLog(#"initWithContentsOfURL 2: %s", ([parser parse])?"OK":"Fail");
parser = [[NSXMLParser alloc]initWithData:[NSData dataWithContentsOfFile:pathToXMLFile]];
NSLog(#"initWithData 1: %s", ([parser parse])?"OK":"Fail");
NSLog(#"initWithData 2: %s", ([parser parse])?"OK":"Fail");
Output
2012-01-24 18:37:31.260 CLIXMPLParser[12658:707] initWithContentsOfURL 1: OK
2012-01-24 18:37:31.280 CLIXMPLParser[12658:707] initWithContentsOfURL 2: Fail
2012-01-24 18:37:31.292 CLIXMPLParser[12658:707] initWithData 1: OK
2012-01-24 18:37:31.297 CLIXMPLParser[12658:707] initWithData 2: OK

Related

Ok in simulator but throwing exception in ios device

when i am running my app in the simulator everything is working perfectly .But when i running the same app in the Ipad exception is being thrown.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil.
In my app at one step i have to request a web-URl and need to parsed the returned JSON response. But I have checked the web-url and have been able to parse perfectly in simulator. But all the problem has been arisen in real ios device.But I think i have identified the code where it is getting wrong.
+ (NSDictionary*) getParsedJSON:(NSString*) urlString {
NSLog(#"################################################################################");
NSLog(#"getParsedJSON => urlString:");
NSLog(#"%#", urlString);
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
NSURLResponse *response1 = nil;
NSError *error = nil;
NSData* response = [NSURLConnection sendSynchronousRequest:request returningResponse:&response1 error:&error];
//NSData* response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(#"--------------------------------------------------------------------------------");
NSString* responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(#"getParsedJSON => responseString:\n%#", responseString);
NSLog(#"--------------------------------------------------------------------------------");
NSError* jsonParsingError = nil;
NSDictionary* parsedJSON = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError]; // here is place where exception seems to be thrown.
if (jsonParsingError) {
NSLog(#"ERROR in parsing JSON: %#", jsonParsingError);
} else {
NSLog(#"getParsedJSON => parsedJSON: \n%#", [parsedJSON description]);
}
NSLog(#"################################################################################");
return parsedJSON;
}
I have identified the line where it seems to be wrong .I have also attached screen shot of the exception report..Hoping for your experienced reply.
AS we can see from the logs your response string is null while you are using it on your Device. This may be due to some internet access problem. Try to Use:
if([response isequaltostring:#"(null)"]||response == nil || response.length == 0)
{
NSError* jsonParsingError = nil;
NSDictionary* parsedJSON = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError]; // here is place where exception seems to be thrown.
if (jsonParsingError) {
NSLog(#"ERROR in parsing JSON: %#", jsonParsingError);
}
else {
NSLog(#"getParsedJSON => parsedJSON: \n%#", [parsedJSON description]);
}
}
Also try to add the exceptional breakpoint and post where exactly the app crashed.
Let me know the result.
First, you need to set an exception breakpoint in Xcode - there are many posts here on how to do that. Second, after each of you statements where an object is created or returned, add an assert:
NSURL *foo = ...
assert(foo);
Doing this will help you find the first issue not the last one.
As per your logs, your response string is empty!
Do the below two things!
Add NSLog(#"Response Data: %#",response); and check if the response has value?
If 'response' has value, convert it to a string - Log the string value - And check if the any of the key has nil value?
'NSJSONSerialization JSONObjectWithData' method would crash if it finds any key with nil value.

Error in calling xml parser method

I am using retailligence barcode api and using nsxml parser method to parse the response below is the code but it didn't give call to parsing method. What's wrong there. Please help
{ NSString *myxmlstr = [NSString stringWithFormat:#"http://apitest.retailigence.com/v1.2/products?apikey=rMMzX5IDYVmTjQ3A7D9sZXukjKiZVmdD&barcode=%#&latitude=37.439097&longitude=-122.175806",brcode];
NSLog(#"my myxmlsstr is %#",myxmlstr);
dataselected = NO;
NSURL * xmlURL = [NSURL fileURLWithPath:myxmlstr];
myParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
myParser.delegate = self;
BOOL success = [myParser parse];
if(success){
NSLog(#"Properly done ");
}
else{
NSLog(#"not done");
}
}
Thanks in advance.
It was an error in code and I have to just replace this code
NSURL * xmlURL = [NSURL fileURLWithPath:myxmlstr];
with this code
NSURL * xmlURL = [NSURL URLWithString:myxmlstr];
This will work.
more information about parsing you get if you call -parserError or use method from protocol - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError to obtain error informations

Solution for a leak in touchXML when no internet and using initWithContentsOfURL

There's a leaking CXMLDocument object shown in instruments everytime, a request to an XML is made to a webservice AND when there's no internet connection available. Here's my code:
NSString *path = ... some URL
NSURL *url = [NSURL URLWithString: path];
CXMLDocument *itemListParser; = [[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil];
... other stuff ...
If we digg deeper and trace initWithContentsOfURL call then we will find this method in "CXMLDocument.m":
- (id)initWithContentsOfURL:(NSURL *)inURL encoding:(NSStringEncoding)encoding options:(NSUInteger)inOptions error:(NSError **)outError
{
if (outError)
*outError = NULL;
NSData *theData = [NSData dataWithContentsOfURL:inURL options:NSUncachedRead error:outError];
if (theData)
{
self = [self initWithData:theData encoding:encoding options:inOptions error:outError];
}
else
{
[self release]; //My suggested fix: We need to release an alloc'ed object because after the "self = null" it will be unable to release it. See the info below.
self = NULL;
}
return(self);
}
It appears, if theData is nil (for example no connection) then self will be nil, and so the result of the call to TouchXML initWithContentsOfURL will be nil too. So, in my code:
CXMLDocument *itemListParser; = [[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil];
I'm alloc'ing a memory but init returns nil, and so itemListParser becomes nil too. So later, trying to release the parser with [itemListParser release] does nothing because release is send to nil.
I was able to fix a leak by adding "[self release]" before "self = NULL" (see the line with my comment in TouchXML initWithContentsOfURL method)

Need help with NSXMLPaser tutorial

At the last part of the tutorial I am having an issue with the users variable, its saying users variable is undeclared.
It is being declared in my NSXMLPaser class that I created as a NSMutableArray and I am "#import"ing the header file of NSXMLPaser class...
Here is the link to the tutorial I am working through, any help would be greatly appreciated.
http://wiki.cs.unh.edu/wiki/index.php/Parsing_XML_data_with_NSXMLParser
- (void) doParse:(NSData *)data {
NSString * filePath = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"xml"];
NSData * fileData = [NSData dataWithContentsOfFile:filePath];
// create and init NSXMLParser object
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:fileData];
// create and init our delegate
XMLParser *parser = [[XMLParser alloc] initXMLParser];
// set delegate
[nsXmlParser setDelegate:parser];
// parsing...
BOOL success = [nsXmlParser parse];
// test the result
if (success) {
NSLog(#"No errors - user count : %i", [parser [users count]]); // users undeclared error here
// get array of users here
// NSMutableArray *users = [parser users];
} else {
NSLog(#"Error parsing document!");
}
[parser release];
[nsXmlParser release];
}
Either you use "[parser.users count]" like Babji said or you could use the dot notation for everything, like "parser.users.count".
Also, you could use "[[parser users] count]". This means you first get the collection users of the parser ("[parser users]") and then you call count on that collection ("[[parser users] count]").
use the users variable [parser.user count]

How to parse xml file that is encoded with another encoder different from utf-8 with using NSXMLParser in objective-C?

Is there a way to parse xml file that is encoded with windows-1254 with using NSXMLParser? When i try, didStartElement method not called.
Code is
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:webData];
XMLParser *parser = [[XMLParser alloc] initXMLParser: objectList];
[parser setReqType:reqType];
[xmlParser setDelegate:parser];
[xmlParser parse];
XML is
<?xml version="1.0" encoding="windows-1254" ?>
<CANLIMACLAR>
<CANLIMACLARROWS>
<TARIH>19/10/2009 21:15</TARIH>
<TAKIM1>Union Berlin</TAKIM1>
<TAKIM2>Fürth</TAKIM2>
<SONUC1>1</SONUC1>
<SONUC2>2</SONUC2>
<DK_DURUM>Maç Sonu</DK_DURUM> ...
</CANLIMACLARROWS> ...
</CANLIMACLAR>
Don't forget if parse() returns NO you can check parseError to see what happened:
if ([parser parse] == NO)
{
NSError *error = [parser parserError];
NSString *readableMessage = [error localizedDescription];
NSLog(#"Error occurred: %#\n", readableMessage);
}
I removed the <?xml version="1.0" encoding="windows-1254" ?> part from xml data and then i send it to xmlParser, so there is no problem anymore.