0

我想将 html 内容解析成字典

编辑:

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。

结尾:

像这样的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];
}

谁做过类似的功能?

4

1 回答 1

0

Keys in a dictionary must be unique. You cannot have more than one "img" key.

Check out this SO question: Objective-C DOM XML parser for iPhone

于 2013-09-17T05:48:54.113 回答