iPhone中需要更快的xml读取

iPhone中需要更快的xml读取

问题描述:

我有以下代码 - 读取xml文件的方法。 但它对我来说工作非常缓慢。 没有足够的方法可以更快地读取&读取数据。iPhone中需要更快的xml读取

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; 
} 

}

让我再次澄清我的问题。

我发现这种阅读xml的方式是不够的。 通过使用这种方式iPhone非常缓慢地加载数据。因为,iphone会每次读&比较每个标签。

我想要一些更快的XML加载&解析。

预先感谢来自adc与我分享你的知识

有一个来自Apple的例子XMLPerformance,它在性能方面说明了libxml与NSXMLParser。查看它的libxml的实现。

的SiesmicXML和TopSongs代码样本显示,导致最少的用户等待时间解析XML的一些方法。

+0

@ennuikiller - 先生,这里的代码示例是可用的? – 2009-09-11 18:04:15

+1

http://developer.apple.com/iphone/library/samplecode/SeismicXML/index.html – ennuikiller 2009-09-12 13:38:50

你看过libxml C库,还是TouchXML包装呢?另外,你确定它是导致你的放缓的NSXMLParser吗?您可能需要使用Instruments或Shark快速检测应用程序并找到热点。