#pragma mark - xml解析(解析完全)
+ (NSMutableDictionary*)parseDire:(NSData*)XMLData
{
CXMLDocument* document = [[CXMLDocument alloc]initWithData:XMLData options:0 error:nil];
CXMLElement* responseNode = (CXMLElement*)[document nodeForXPath:@"//response" error:nil];
NSMutableDictionary* dataDict = [[NSMutableDictionary alloc]init];
NSMutableDictionary* content = [self getJsonData:responseNode andDictionary:dataDict];
// NSLog(@"dataDict:%@",content);
return content;
}
+ (NSMutableDictionary*)getJsonData:(CXMLElement*)element andDictionary:(NSDictionary*)dict
{
NSMutableDictionary* elementDict = [[NSMutableDictionary alloc]initWithDictionary:dict];
//NSLog(@"stringValue:%@",element.stringValue);
if ([element isKindOfClass:[CXMLElement class]]) {
//NSLog(@"1stringValue:%@",element.stringValue);
if (element.childCount > 1) {
//NSLog(@"2stringValue:%@",element.stringValue);
CXMLElement* oneEle = [element.children objectAtIndex:0];
CXMLElement* twoEle = [element.children objectAtIndex:1];
if ([oneEle.name isEqualToString:twoEle.name]) {
NSMutableArray* arr = [[NSMutableArray alloc]init];
for(CXMLElement* elem in element.children)
{
NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
for (CXMLElement* ele in elem.children) {
//NSLog(@"key:%@ == value:%@ ele.childCount:%lu",ele.stringValue,ele.name,(unsigned long)ele.childCount);
if (ele.childCount > 1 && [[ele.children objectAtIndex:0] isKindOfClass:[CXMLElement class]]) {
dict = [self getJsonData:ele andDictionary:dict];
}
else if(ele.childCount == 1 && [[ele.children objectAtIndex:0] isKindOfClass:[CXMLElement class]])
{
dict = [self getJsonData:ele andDictionary:dict];
}
else if(ele.childCount == 1)
[dict setObject:ele.stringValue forKey:ele.name];
}
[arr addObject:dict];
}
[elementDict setObject:arr forKey:element.name];
}
else{
NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
for(CXMLElement* ele in element.children)
{
//NSLog(@"key:%@ == value:%@ ele.childCount:%lu",ele.stringValue,ele.name,(unsigned long)ele.childCount);
if (ele.childCount >1 && [[ele.children objectAtIndex:0] isKindOfClass:[CXMLElement class]]) {
dict = [self getJsonData:ele andDictionary:dict];
}
else if(ele.childCount == 1 && [[ele.children objectAtIndex:0] isKindOfClass:[CXMLElement class]])
{
dict = [self getJsonData:ele andDictionary:dict];
}
else if (ele.childCount == 1 && ![[ele.children objectAtIndex:0] isKindOfClass:[CXMLElement class]])
{
[dict setObject:ele.stringValue forKey:ele.name];
}
}
[elementDict setObject:dict forKey:element.name];
}
}
else if(element.childCount == 1 && ![[element.children objectAtIndex:0] isKindOfClass:[CXMLElement class]]){
//NSLog(@"key:%@ == value:%@ element.childCount:%lu",element.stringValue,element.name,(unsigned long)element.childCount);
//NSLog(@"3stringValue:%@",element.stringValue);
[elementDict setObject:element.stringValue forKey:element.name];
}
else if(element.childCount == 1 && [[element.children objectAtIndex:0] isKindOfClass:[CXMLElement class]])
{
//NSLog(@"4stringValue:%@ element.name:%@",element.stringValue,element.name);
if ([element.name isEqualToString:@"cityconf"]||[element.name isEqualToString:@"version"]||[element.name isEqualToString:@"isaddidcard"]||[element.name isEqualToString:@"dadastation"]||[element.name isEqualToString:@"station"]) {
NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
for(CXMLElement* ele in element.children)
{
//NSLog(@"key:%@ == value:%@ ele.childCount:%lu",ele.stringValue,ele.name,(unsigned long)ele.childCount);
if (ele.childCount >1 && [[ele.children objectAtIndex:0] isKindOfClass:[CXMLElement class]]) {
dict = [self getJsonData:ele andDictionary:dict];
}
else if(ele.childCount == 1 && [[ele.children objectAtIndex:0] isKindOfClass:[CXMLElement class]])
{
dict = [self getJsonData:ele andDictionary:dict];
}
else if (ele.childCount == 1 && ![[ele.children objectAtIndex:0] isKindOfClass:[CXMLElement class]])
{
[dict setObject:ele.stringValue forKey:ele.name];
}
}
[elementDict setObject:dict forKey:element.name];
}else{
// NSMutableDictionary* dic = [[NSMutableDictionary alloc]init];
NSMutableArray *arr = [[NSMutableArray alloc] init];
for(CXMLElement* elem in element.children)
{
NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
for (CXMLElement* ele in elem.children) {
//NSLog(@"key:%@ == value:%@ ele.childCount:%lu",ele.stringValue,ele.name,(unsigned long)ele.childCount);
if (ele.childCount > 1 && [[ele.children objectAtIndex:0] isKindOfClass:[CXMLElement class]]) {
dict = [self getJsonData:ele andDictionary:dict];
}
else if(ele.childCount == 1 && [[ele.children objectAtIndex:0] isKindOfClass:[CXMLElement class]])
{
dict = [self getJsonData:ele andDictionary:dict];
}
else if(ele.childCount == 1)
[dict setObject:ele.stringValue forKey:ele.name];
}
// [dic setObject:dict forKey:elem.name];
[arr addObject:dict];
}
// [elementDict setObject:dic forKey:element.name];
[elementDict setObject:arr forKey:element.name];
}
}else{
//NSLog(@"5stringValue:%@",element.stringValue);
// CXMLElement* elem = [element.children objectAtIndex:0];
// [elementDict setObject:elem.stringValue forKey:elem.name];
[elementDict setObject:element.stringValue forKey:element.name];
}
}
else{
//NSLog(@"6stringValue:%@",element.stringValue);
}
return elementDict;
}