View Code
1 +(NSDictionary *) XmlAnalysisWithString:(NSString *) URLString 2 3 { 4 5 NSError* error; 6 7 NSMutableDictionary * dataDic = [NSMutableDictionary dictionary]; 8 9 //拿出xml文件内容 10 11 NSURL * myURL = [NSURL URLWithString:URLString]; 12 13 NSString* fileContent = [[NSString alloc] initWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error]; 14 15 GDataXMLDocument* document = [[GDataXMLDocument alloc] initWithXMLString:fileContent options:1 error:&error]; 16 17 18 19 if (error) { 20 21 NSLog(@”%@”, [error localizedDescription]); 22 23 } else { 24 25 //取出xml的根节点 26 27 GDataXMLElement* rootElement = [document rootElement]; 28 29 GDataXMLElement * testElement = nil; 30 31 GDataXMLElement * childTestElement = nil; 32 33 //取出根节点的所有孩子节点 34 35 for (int i = 0; i < [[rootElement children] count]; i++) { 36 37 testElement = [[rootElement children]objectAtIndex:i]; 38 39 //获取channelname 40 41 NSString * titleContent = [[[testElement elementsForName:@"channelname"]objectAtIndex:0] stringValue]; 42 43 NSArray * children = [testElement children]; 44 45 NSMutableArray * allData = [[NSMutableArray alloc]init]; 46 47 //从第2个节点开始都是item ,第一个节点是每一个的channelname 48 49 for (int j = 1; j < [children count]; j++) 50 51 { 52 53 NSMutableDictionary * everyDic = [[NSMutableDictionary alloc]init]; 54 55 childTestElement = [children objectAtIndex:j]; 56 57 //获得每一个item下的有用信息 58 59 NSString * topicContent = [[[childTestElement elementsForName:@"topic"]objectAtIndex:0] stringValue] ; 60 61 NSString * summaryContent = [[[childTestElement elementsForName:@"summary"]objectAtIndex:0] stringValue] ; 62 63 NSString * picContent = [[[childTestElement elementsForName:@"pic"]objectAtIndex:0] stringValue] ; 64 65 NSString * infoidContent = [[[childTestElement elementsForName:@"infoid"]objectAtIndex:0] stringValue] ; 66 67 68 69 [everyDic setObject:topicContent forKey:@"topic"]; 70 71 [everyDic setObject:summaryContent forKey:@"summary"]; 72 73 [everyDic setObject:picContent forKey:@"pic"]; 74 75 [everyDic setObject:infoidContent forKey:@"infoid"]; 76 77 78 79 //把每一个item加进每一个channel的数组中 80 81 [allData addObject:everyDic]; 82 83 } 84 85 //根据channel的名字存储数组 86 87 [dataDic setObject:allData forKey:titleContent]; 88 89 } 90 91 } 92 93 return dataDic; 94 95 }