@interface Home_ViewController ()<NSXMLParserDelegate>//不要忘了代理
//xml解析需要的 变量
@property(nonatomic,strong)NSMutableArray* cityInfo_MutArray;
@property(nonatomic,copy)NSString *currentTag;
@property(nonatomic,copy)NSString* currentString;
@property(nonatomic,strong)Model_City *tmpCity;
@end
@implementation Home_ViewController
/*xml文件的格式如下:
<RECORD>
<id>1</id>
<parent_id>0</parent_id>
<name>中国</name>
<level>0</level>
<status>0</status>
</RECORD>
<RECORD>
<id>2</id>
<parent_id>1</parent_id>
<name>北京</name>
<level>1</level>
<status>0</status>
</RECORD>
*/
- (void)viewDidLoad
{
[super viewDidLoad];
//xml解析 方法
[self receive_CityInfo_XML];
}
//---------------解析XML----开始----------------
//从本地xml文件中解析城市信息
-(void)receive_CityInfo_XML
{
//xml文件的路径
NSString* path=[[NSBundle mainBundle] pathForResource:@"zdk_city" ofType:@"xml"];
NSFileHandle* file=[NSFileHandle fileHandleForReadingAtPath:path];
NSData* data=[file readDataToEndOfFile];
NSString* stringData=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
DLog(@"dataStr=%@",stringData);
NSData* data2=[stringData dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser* xmlRead=[[NSXMLParser alloc] initWithData:data2];
[xmlRead setDelegate:self];//设置NSXMLParser对象的解析方法代理
[xmlRead parse];//调用代理解析NSXMLParser对象,看解析是否成功
//关闭文件控制器
[file closeFile];
//把获得的 全部城市信息,保存到本地sqlite数据库中
if(self.cityInfo_MutArray.count>0)
{
//这些是自定义的类 和 方法
SqLite_Options* tmpOptions=[[SqLite_Options alloc] init];
for (Model_City* tmp in self.cityInfo_MutArray)
{
[tmpOptions insert_City:tmp];
}
}
}
//下面是NSXMLParser的代理方法
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
//解析 开始时 执行该方法
}
//
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary *)attributeDict
{
self.currentTag = elementName;
//RECORDS是这个xml文件最外层的标签
if ([elementName isEqualToString:@"RECORDS"])
{
self.cityInfo_MutArray = [[NSMutableArray alloc] init];
}else if ([elementName isEqualToString:@"RECORD"])
{
self.tmpCity = [[Model_City alloc] init];
}
}
//该代理方法 获取每个标签的内容
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (self.currentString == nil)
{
self.currentString = @"";
}
//如果是RECORD标签内部的标签
if ([self.currentTag isEqualToString:@"name"] ||
[self.currentTag isEqualToString:@"id"] ||
[self.currentTag isEqualToString:@"parent_id"] ||
[self.currentTag isEqualToString:@"level"]||
[self.currentTag isEqualToString:@"status"])
{
// 注意!!每个标签都会进入此处两次,但是第二次进入时string=@"\n",
//所以要进行两个条件的约束[self.currentString isEqualToString:@""]&&![string isEqualToString:@"\n"]
if ([self.currentString isEqualToString:@""]&&![string isEqualToString:@"\n"])
{
DLog(@"currentTag=%@,string=%@",self.currentTag,string);
self.currentString=[NSString stringWithFormat:@"%@",string];
}
}
}
//每个标签结束时执行,在此代理 方法中 把xml中的 对应的标签的值传给self.tmpCity,最后把self.tmpCity保存到数组中
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"RECORD"])
{
[self.cityInfo_MutArray addObject:self.tmpCity];
self.tmpCity=nil;//
} if ([elementName isEqualToString:@"name"])
{
self.tmpCity.cityName = [self.currentString copy];
self.currentString=@"";
}else if ([elementName isEqualToString:@"id"])
{
self.tmpCity.cityID = [self.currentString copy];
self.currentString=@"";
}else if ([elementName isEqualToString:@"parent_id"])
{
self.tmpCity.cityParent_id = [self.currentString copy];
self.currentString=@"";
}else if ([elementName isEqualToString:@"level"])
{
self.tmpCity.cityLevel = [self.currentString copy];
self.currentString=@"";
}else if ([elementName isEqualToString:@"status"])
{
self.tmpCity.cityStatus = [self.currentString copy];
self.currentString=@"";
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
DLog(@"解析xml文件完成");
}
//-----------------解析XML----结束----------------------
@end