xml转plist
把以下的xml文件
<?xml version="1.0" encoding="utf-8"?>
<hotelCities>
<city>
<type>HOT</type>
<cityname>热门城市</cityname>
</city>
<city>
<type>HOT</type>
<cityname>北京</cityname>
<citycode>PEK</citycode>
<cityspell>beijing</cityspell>
</city>
<city>
<type>HOT</type>
<cityname>上海</cityname>
<citycode>SHA</citycode>
<cityspell>shanghai</cityspell>
</city>
<city>
<type>HOT</type>
<cityname>广州</cityname>
<citycode>CAN</citycode>
<cityspell>guangzhou</cityspell>
</city>
<city>
<type>A</type>
<cityname>A</cityname>
</city>
<city>
<type>A</type>
<cityname>阿坝州</cityname>
<citycode>ABA</citycode>
<cityspell>abazhou</cityspell>
</city>
<city>
<type>A</type>
<cityname>阿城</cityname>
<citycode>ACH</citycode>
<cityspell>acheng</cityspell>
</city>
...
</hotelCities>
转成以下plist文件
代码如下:
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], @"hotelCity.xml"]];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:plistXML options:0 error:&error];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:350];
NSArray *type = [doc nodesForXPath:@"//hotelCities/city/type" error:nil];
NSArray *cityname = [doc nodesForXPath:@"//hotelCities/city/cityname" error:nil];
NSArray *citycode = [doc nodesForXPath:@"//hotelCities/city/citycode" error:nil];
NSArray *cityspell = [doc nodesForXPath:@"//hotelCities/city/cityspell" error:nil];
NSMutableArray *cityHot = [[NSMutableArray alloc] init];
NSMutableArray *cityA = [[NSMutableArray alloc] init];
NSMutableArray *cityB = [[NSMutableArray alloc] init];
NSMutableArray *cityC = [[NSMutableArray alloc] init];
NSMutableArray *cityD = [[NSMutableArray alloc] init];
NSMutableArray *cityE = [[NSMutableArray alloc] init];
NSMutableArray *cityF = [[NSMutableArray alloc] init];
NSMutableArray *cityG = [[NSMutableArray alloc] init];
NSMutableArray *cityH = [[NSMutableArray alloc] init];
NSMutableArray *cityI = [[NSMutableArray alloc] init];
NSMutableArray *cityJ = [[NSMutableArray alloc] init];
NSMutableArray *cityK = [[NSMutableArray alloc] init];
NSMutableArray *cityL = [[NSMutableArray alloc] init];
NSMutableArray *cityM = [[NSMutableArray alloc] init];
NSMutableArray *cityN = [[NSMutableArray alloc] init];
NSMutableArray *cityO = [[NSMutableArray alloc] init];
NSMutableArray *cityP = [[NSMutableArray alloc] init];
NSMutableArray *cityQ = [[NSMutableArray alloc] init];
NSMutableArray *cityR = [[NSMutableArray alloc] init];
NSMutableArray *cityS = [[NSMutableArray alloc] init];
NSMutableArray *cityT = [[NSMutableArray alloc] init];
NSMutableArray *cityU = [[NSMutableArray alloc] init];
NSMutableArray *cityV = [[NSMutableArray alloc] init];
NSMutableArray *cityW = [[NSMutableArray alloc] init];
NSMutableArray *cityX = [[NSMutableArray alloc] init];
NSMutableArray *cityY = [[NSMutableArray alloc] init];
NSMutableArray *cityZ = [[NSMutableArray alloc] init];
NSInteger num = [type count];
NSInteger titleCount = 0;
for (NSInteger idx = 0; idx < num; ++idx) {
NSString *typeString = [[type objectAtIndex:idx] stringValue];
NSString *cityName = [[cityname objectAtIndex:idx] stringValue];
if ([@"热门城市" isEqualToString:[[cityname objectAtIndex:idx] stringValue]]||
[@"A" isEqualToString:cityName]||
[@"B" isEqualToString:cityName]||
[@"C" isEqualToString:cityName]||
[@"D" isEqualToString:cityName]||
[@"E" isEqualToString:cityName]||
[@"F" isEqualToString:cityName]||
[@"G" isEqualToString:cityName]||
[@"H" isEqualToString:cityName]||
[@"I" isEqualToString:cityName]||
[@"J" isEqualToString:cityName]||
[@"K" isEqualToString:cityName]||
[@"L" isEqualToString:cityName]||
[@"M" isEqualToString:cityName]||
[@"N" isEqualToString:cityName]||
[@"O" isEqualToString:cityName]||
[@"P" isEqualToString:cityName]||
[@"Q" isEqualToString:cityName]||
[@"R" isEqualToString:cityName]||
[@"S" isEqualToString:cityName]||
[@"T" isEqualToString:cityName]||
[@"U" isEqualToString:cityName]||
[@"V" isEqualToString:cityName]||
[@"W" isEqualToString:cityName]||
[@"X" isEqualToString:cityName]||
[@"Y" isEqualToString:cityName]||
[@"Z" isEqualToString:cityName]) {
titleCount += 1;
}else {
NSString *citycodeString = [[citycode objectAtIndex:idx-titleCount] stringValue];
NSString *cityspellString = [[cityspell objectAtIndex:idx-titleCount] stringValue];
NSMutableArray *city = [[NSMutableArray alloc] init];
[city addObject:cityName];
[city addObject:citycodeString];
[city addObject:cityspellString];
if ([@"HOT" isEqualToString:typeString]) {
[cityHot addObject:city];
}else if ([@"A" isEqualToString:typeString]) {
[cityA addObject:city];
}else if ([@"B" isEqualToString:typeString]) {
[cityB addObject:city];
}else if ([@"C" isEqualToString:typeString]) {
[cityC addObject:city];
}else if ([@"D" isEqualToString:typeString]) {
[cityD addObject:city];
}else if ([@"E" isEqualToString:typeString]) {
[cityE addObject:city];
}else if ([@"F" isEqualToString:typeString]) {
[cityF addObject:city];
}else if ([@"G" isEqualToString:typeString]) {
[cityG addObject:city];
}else if ([@"H" isEqualToString:typeString]) {
[cityH addObject:city];
}else if ([@"I" isEqualToString:typeString]) {
[cityI addObject:city];
}else if ([@"J" isEqualToString:typeString]) {
[cityJ addObject:city];
}else if ([@"K" isEqualToString:typeString]) {
[cityK addObject:city];
}else if ([@"L" isEqualToString:typeString]) {
[cityL addObject:city];
}else if ([@"M" isEqualToString:typeString]) {
[cityM addObject:city];
}else if ([@"N" isEqualToString:typeString]) {
[cityN addObject:city];
}else if ([@"O" isEqualToString:typeString]) {
[cityO addObject:city];
}else if ([@"P" isEqualToString:typeString]) {
[cityP addObject:city];
}else if ([@"Q" isEqualToString:typeString]) {
[cityQ addObject:city];
}else if ([@"R" isEqualToString:typeString]) {
[cityR addObject:city];
}else if ([@"S" isEqualToString:typeString]) {
[cityS addObject:city];
}else if ([@"T" isEqualToString:typeString]) {
[cityT addObject:city];
}else if ([@"U" isEqualToString:typeString]) {
[cityU addObject:city];
}else if ([@"V" isEqualToString:typeString]) {
[cityV addObject:city];
}else if ([@"W" isEqualToString:typeString]) {
[cityW addObject:city];
}else if ([@"X" isEqualToString:typeString]) {
[cityX addObject:city];
}else if ([@"Y" isEqualToString:typeString]) {
[cityY addObject:city];
}else if ([@"Z" isEqualToString:typeString]) {
[cityZ addObject:city];
}
[city release];
}
//[dict setObject:[[value objectAtIndex:idx] stringValue] forKey:[[key objectAtIndex:idx] stringValue]];
//NSLog(@"%@:%@\n", [[value objectAtIndex:idx] stringValue], [[key objectAtIndex:idx] stringValue]);
}
[dict setObject:cityHot forKey:@"hot"];
[dict setObject:cityA forKey:@"A"];
[dict setObject:cityB forKey:@"B"];
[dict setObject:cityC forKey:@"C"];
[dict setObject:cityD forKey:@"D"];
[dict setObject:cityE forKey:@"E"];
[dict setObject:cityF forKey:@"F"];
[dict setObject:cityG forKey:@"G"];
[dict setObject:cityH forKey:@"H"];
[dict setObject:cityI forKey:@"I"];
[dict setObject:cityJ forKey:@"J"];
[dict setObject:cityK forKey:@"K"];
[dict setObject:cityL forKey:@"L"];
[dict setObject:cityM forKey:@"M"];
[dict setObject:cityN forKey:@"N"];
[dict setObject:cityO forKey:@"O"];
[dict setObject:cityP forKey:@"P"];
[dict setObject:cityQ forKey:@"Q"];
[dict setObject:cityR forKey:@"R"];
[dict setObject:cityS forKey:@"S"];
[dict setObject:cityT forKey:@"T"];
[dict setObject:cityU forKey:@"U"];
[dict setObject:cityV forKey:@"V"];
[dict setObject:cityW forKey:@"W"];
[dict setObject:cityX forKey:@"X"];
[dict setObject:cityY forKey:@"Y"];
[dict setObject:cityZ forKey:@"Z"];
[dict writeToFile:[NSString stringWithFormat:@"/%@", @"city.plist"] atomically:YES];