---恢复内容开始---
/* <tweet> <id>6462192</id> <portrait> http://static.oschina.net/uploads/user/587/1175932_50.jpg?t=1418648003000 </portrait> <author> <![CDATA[ fywtat ]]> </author> <authorid>1175932</authorid> <body> <![CDATA[ 又被赶出来睡沙发 :smile: ]]> </body> <attach/> <appclient>3</appclient> <commentCount>1</commentCount> <pubDate>2015-09-01 00:05:27</pubDate> <imgSmall/> <imgBig/> <likeCount>2</likeCount> <isLike>0</isLike> <likeList>...</likeList> </tweet> */ ===================================== #pragma mark - 下载解析数据 -(void)startDownloadData { NSString *urlString = OCTweetURL; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { //解析数据 GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:responseObject options:0 error:nil]; if(doc) { NSArray *tweets = [doc nodesForXPath:@"//tweet" error:nil]; for (GDataXMLElement *element in tweets) { TweetModel *model = [[TweetModel alloc] init]; for (GDataXMLElement *subElement in element.children) { [model setValue:subElement.stringValue forKey:subElement.name]; } [_dataArray addObject:model]; } [self.tableView reloadData]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error = %@",error); }]; } //根据传入的结点创建model代码 -(void)createModelCodeByElement:(GDataXMLElement *)element modelName:(NSString *)name { printf("\[email protected] %s : NSObject\n",name.UTF8String); for (GDataXMLElement *subElement in element.children) { printf("@property (copy,nonatomic) NSString *%s;\n",subElement.name.UTF8String); } printf("@end\n"); }
<oschina> <tweetCount>500</tweetCount> <!-- 总动弹数 --> <pagesize>20</pagesize> <tweets> <tweet> <id>6463329</id> <portrait> http://static.oschina.net/uploads/user/716/1433031_50.jpg?t=1433144307000 </portrait> <author> <![CDATA[ ahhg ]]> </author> <authorid>1433031</authorid> <body> <![CDATA[ 技术水平不上不下,有点迷茫了 ]]> </body> <attach/> <appclient>1</appclient> <commentCount>0</commentCount> <pubDate>2015-09-01 09:44:43</pubDate> <imgSmall/> <imgBig/> <likeCount>0</likeCount> <isLike>0</isLike> <likeList></likeList> </tweet> <tweet>...</tweet> <tweet>...</tweet> <tweet>
解析数据
GData
-(void)startDownloadData { NSString *urlString = OCTweetURL; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { //解析数据 GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:responseObject options:0 error:nil]; if(doc) { //存取tweet节点的数组 NSArray *tweets = [doc nodesForXPath:@"//tweet" error:nil]; //遍历单个tweet节点,一个element属于一个tweet节点 for (GDataXMLElement *element in tweets) { TweetModel *model = [[TweetModel alloc] init]; //遍历element(tweet)的子节点 element.children 属于一个数组 //<id>6463329</id> 一个subElement节点的键与值 for (GDataXMLElement *subElement in element.children) { [model setValue:subElement.stringValue forKey:subElement.name]; } [_dataArray addObject:model]; } [self.tableView reloadData]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error = %@",error); }]; }
系统XML解析
<?xml version="1.0" encoding="UTF-8"?> <!-- 每个XML文档都由XML序言开始,告诉解析器或浏览器这个文件应该按照XML规则进行解析 --> <!-- XML标签没有被预定义,你需要自行定义标签。XML被设计为具有自我描述性 --> <qfclass> <student> <name>优衣库</name> <address>北京三里屯</address> </student> <student> <name>黑人</name> <address>美国加利福尼亚</address> </student> <student> <name>毛爷爷</name> <address>中国精英</address> </student> <student> <name>葫芦小金刚</name> <address>葫芦娃</address> </student> <student> <name>蛇精</name> <address>报告大王</address> </student> </qfclass>
XML
1 #import "CLJXMLParser.h" 2 #import "Student.h" 3 4 @interface MCJXMLParser ()<NSXMLParserDelegate> 5 { 6 NSMutableString *_elementStr; 7 Student *_student; 8 9 } 10 @end 11 12 @implementation MCJXMLParser 13 - (void)test 14 { 15 NSString *path = @"/Users/ms/Desktop/备课/0814/test.xml"; 16 NSData *data = [NSData dataWithContentsOfFile:path]; 17 18 // 解析器 19 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; 20 parser.delegate = self; 21 22 // 开始解析 23 [parser parse]; 24 } 25 26 #pragma mark - NSXMLParserDelegate 27 28 #pragma mark 开始解析文档,在这里做初始化工作 29 - (void)parserDidStartDocument:(NSXMLParser *)parser 30 { 31 _arrayM = [NSMutableArray array]; 32 _elementStr = [NSMutableString string]; 33 } 34 35 #pragma mark 开始读取节点 清空缓存数据 36 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 37 { 38 if ([elementName isEqualToString:@"student"]) { 39 _student = [[Student alloc] init]; 40 } 41 // 清空拼接字符串 42 [_elementStr setString:@""]; 43 } 44 45 #pragma mark 循环查找节点内容 46 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 47 { 48 // 拼接字符串 49 [_elementStr appendString:string]; 50 } 51 52 #pragma mark 一个节点完成 53 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 54 { 55 NSString *result = [NSString stringWithString:_elementStr]; 56 if ([elementName isEqualToString:@"student"]) { 57 [_arrayM addObject:_student]; 58 } else if ([elementName isEqualToString:@"name"]){ 59 _student.name = result; 60 } else if ([elementName isEqualToString:@"address"]) { 61 _student.address = result; 62 } 63 } 64 65 #pragma mark 解析完成,处理数据 66 - (void)parserDidEndDocument:(NSXMLParser *)parser 67 { 68 NSLog(@"%@", _arrayM); 69 } 70 71 #pragma mark 解析出错,清理数据 72 - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 73 { 74 // 解析发生错误,需要把解析过程中生成数据清除,否则会出现不必要的麻烦 75 [_arrayM removeAllObjects]; 76 } 77 @end
NSXMLParser解析
---恢复内容结束---
时间: 2024-10-13 22:14:56