我有以下代码 - 读取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 Loading&解析。
提前感谢您与我分享您的知识
答案 0 :(得分:3)
Apple有一个名为XMLPerformance的例子,它说明了libxml与NSXMLParser的性能。查看它的libxml实现。
答案 1 :(得分:2)
来自adc的SiesmicXML和TopSongs代码示例显示了解析XML的一些方法,这些方法导致用户等待时间最短。
答案 2 :(得分:2)