Question

I have following code - methods to read xml files. But it works very slow for me. Isn't there any sufficient way to fetch & read data faster.

if(connectionRemaining)
{
    [self LoadingPopUp];
    NSURL *tmpURl=[NSURL URLWithString:[NSString stringWithFormat:@"%@getcategory.php",[iGolfAppDelegate getServerPath]]];
    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:tmpURl];
    NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if(conn)
        myWebData=[[NSMutableData data] retain];
    connectionRemaining=NO;
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[myWebData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[myWebData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
//  NSString *theXML = [[NSString alloc] initWithBytes: [myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding];
//  NSLog(@"%@",theXML);[theXML release];   
if( myXMLParser )
    [myXMLParser release];
myXMLParser = [[NSXMLParser alloc] initWithData: myWebData];
[myXMLParser setDelegate: self]; [myXMLParser setShouldResolveExternalEntities: YES];
[myXMLParser parse];[connection release];[myWebData release];
}

#pragma mark
#pragma mark XMLParsing Methods
-(void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName attributes:(NSDictionary*)attributeDict {
if([elementName isEqualToString:@"category"])
    categoryArray=[[NSMutableArray alloc]init];
else if([elementName isEqualToString:@"Prop_Category"])
    aCategory=[[Category alloc] init];
}

-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string {
if(!currentElementValue)
    currentElementValue=[[NSMutableString alloc] initWithString:string];
else
    [currentElementValue appendString:string];
}
-(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName {
if([elementName isEqualToString:@"category"])
{ [LoadingAlert dismissWithClickedButtonIndex:0 animated:YES]; [LoadingAlert release];[self categoryPickerDiplay]; return;  }
else if([elementName isEqualToString:@"Prop_Category"])
{ [categoryArray addObject:aCategory];[aCategory release];aCategory=nil; }
else{   
    [aCategory setValue:currentElementValue forKey:elementName];
    [currentElementValue release];currentElementValue=nil;
}

}

Let me clarify my question again.

I have observed that this way of reading xml is not sufficient. By using this way iPhone loads data very slowly. Because, iphone will read & compare each tag every time.

I want some faster XML Loading & Parsing.

Thanks in advance for sharing your knowledge with me

Was it helpful?

Solution

There's an example from Apple called XMLPerformance that illustrates libxml vs. NSXMLParser in terms of performance. Check out its implementation of libxml.

OTHER TIPS

The SiesmicXML and TopSongs code samples from adc show some ways of parsing XML that cause a minimum of user wait time.

Have you looked at the libxml C library, or the TouchXML wrapper for it? Also, are you sure that it's the NSXMLParser that's causing your slowdown? You might want to do a quick runthrough using Instruments or Shark to profile the application and find the hotspots.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top