本文综合两篇文章后总结出来的,下面贴上地址,且这两篇文章将解析xml原理说得很清楚,这里只贴代码
这是解析网络xml的:http://www.cnblogs.com/wendingding/p/3815497.html
这是解析本地网路xml的:http://blog.csdn.net/sjzsp/article/details/6314740
代码如下:
主程序:
confObject=[[ConfObjectalloc]init];
tempString=[[NSMutableStringalloc]init];
//创建路径
NSString *urlStr=@"Url地址";
NSURL *url=[NSURLURLWithString:urlStr];
//创建请求
NSMutableURLRequest *request=[NSMutableURLRequestrequestWithURL:url];//默认为get请求
//设置最大的网络等待时间
request.timeoutInterval=20.0;
//获取主队列
NSOperationQueue *queue=[NSOperationQueuemainQueue];
//发起请求
[NSURLConnectionsendAsynchronousRequest:requestqueue:queue completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError) {
if (data) {//如果请求成功,拿到服务器返回的数据
NSLog(@"Start to parse service XML");
[selfparseXMLData:data];
}else {
[selfparseLocalXML];
}
}];
下面是一些调用的方法:
-(void)parseXMLData:(NSData *)data{
NSXMLParser *parser=[[NSXMLParseralloc]initWithData:data];
parser.delegate=self;
[parserparse];
[parserrelease];
}
-(void)parseLocalXML{
NSLog(@"Start to parse Local XML");
NSString *xmlFilePath = [[NSBundlemainBundle]pathForResource:@"conf"ofType:@"xml"];
NSFileHandle *file = [NSFileHandlefileHandleForReadingAtPath:xmlFilePath];
NSData *data = [filereadDataToEndOfFile];
[filecloseFile];
NSXMLParser *m_parser = [[NSXMLParseralloc]initWithData:data];
[m_parsersetDelegate:self];
BOOL flag = [m_parserparse];
if(flag) {
NSLog(@"获取指定路径的xml文件成功");
}else{
NSLog(@"获取指定路径的xml文件失败");
}
[m_parserrelease];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
NSLog(@"elementName %@",elementName);
[tempString setString:@""];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[tempStringappendString:string];
NSLog(@"data:%@",string);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementNameisEqualToString:@"version"]){
confObject.version=[tempStringintValue];
}elseif ([elementNameisEqualToString:@"meriod"]){
confObject.meriod=[tempStringintValue];
}elseif ([elementNameisEqualToString:@"upload"]){
confObject.upload=[tempStringintValue];
}elseif ([elementNameisEqualToString:@"rUrl"]){
confObject.rUrl=tempString;
}elseif ([elementNameisEqualToString:@"cUrl"]){
confObject.cUrl=tempString;
}elseif ([elementNameisEqualToString:@"aUrl"]){
confObject.aUrl=tempString;
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"parese XML ends");
NSArray* xmlData = [tempStringcomponentsSeparatedByString: @","];
fstconfObject.version=[[xmlDataobjectAtIndex:0]integerValue];
fstconfObject.measurementPeriod=[[xmlDataobjectAtIndex:1]integerValue];
fstconfObject.uploadPeriod=[[xmlDataobjectAtIndex:2]integerValue];
fstconfObject.requestUrl=[xmlDataobjectAtIndex:3];
fstconfObject.configUrl=[xmlDataobjectAtIndex:4];
fstconfObject.apkUrl=[xmlDataobjectAtIndex:5];
NSLog(@"version:%d",fstconfObject.version);
NSLog(@"measurementPeriod: %d",fstconfObject.measurementPeriod);
NSLog(@"uploadPeriod: %d",fstconfObject.uploadPeriod);
NSLog(@"requestUrl: %@",fstconfObject.requestUrl);
NSLog(@"configUrl %@:",fstconfObject.configUrl);
NSLog(@"apkUrl: %@",fstconfObject.apkUrl);
parseXMLEnd=YES;
}