IOS Regular解析HTML

时间:2013-09-17 04:00:18

标签: html ios

我想将html内容解析为Dictionary

编辑:

I need to parse just simple HTML, don't need to consider the complex situation.
WEB side: when I was in the system input information, using the HTML editor. But fix the old WEB system , need to modify the place more, so temporary use parsing HTML mode in the current version of the APP。

END:

Html就像这样:

<p>hahaha</p><img src="aaaa.jpg"/>heihei<img src="bbb.jpg"/>guagua

我希望结果是:

text hahaha
img  aaaa.jpg
text heihei
img  bbb.jpg
text guagua

我的代码是:

   //<p>hahaha</p><img src="aaaa.jpg"/>heihei<img src="bbb.jpg"/>guagua
   //for this
  //NSArray = {0,1,2,3,4}
  //NSDictionary{Sort-Key,V}={{0,{text,hahaha}},{1,{img,aaaa.jpg}},{2,{text,heihei}},{3,     {img,bbb.jpg}},{4,{text,guagua}}}

 -(NSArray*)RegularExpression:(NSString *)str dic:(NSMutableDictionary**)dic
{
     if(str == nil) return nil;
     NSString *pgnText = str;
     NSString* tags=@"<[p|div].*?>(.*?)</[p|div].*?>";
     NSString *regTags = tags;
     NSError *error;
     NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags                                                                         options:NSRegularExpressionCaseInsensitive error:&error];
     NSArray *matches = [regex matchesInString:pgnText
                                  options:0
                                    range:NSMakeRange(0, [pgnText length])];

     NSMutableArray* arrItems = [[NSMutableArray alloc] initWithCapacity:[matches count]];
     if(matches.count >0){
         for (NSTextCheckingResult *match in matches) {
             NSString *tagValue = [pgnText substringWithRange:[match rangeAtIndex:1]];
             NSArray* arr = [self RegularExpression:tagValue dic:dic];
             [arrItems addObjectsFromArray:arr];
        }
    }
    else{
        NSString* regTags2 = @".*?<img.*?src.*?=.*?[\"|”](.*?)[\"|”].*?/>";
        NSRegularExpression *regex2 = [NSRegularExpression     regularExpressionWithPattern:regTags2            options:NSRegularExpressionCaseInsensitive|NSRegularExpressionAnchorsMatchLines                                                            error:&error];
        pgnText = str;
        NSArray *matches2 = [regex2 matchesInString:pgnText
                             options:0
                               range:NSMakeRange(0, [pgnText length])];
        for (NSTextCheckingResult *match in matches2) {
            NSString *tagValue = [pgnText substringWithRange:[match rangeAtIndex:1]];
            NSLog(@"%@",tagValue);
        }
    }
    return [arrItems autorelease];
}

谁做过类似的功能?

1 个答案:

答案 0 :(得分:0)

字典中的键必须是唯一的。你不能拥有多个“img”键。

查看此SO问题:Objective-C DOM XML parser for iPhone