【问题标题】:Text + Images in CDATA BlockCDATA 块中的文本 + 图像
【发布时间】:2013-02-14 10:38:02
【问题描述】:

所以我需要从 CDATA 块中解析数据。

它看起来像

<![CDATA[Important text I need<span style=" color:#000000;"><img src="imageName.jpg" alt="imageName" border=0 style="vertical-align:text-bottom;" /></span>Still important text]]>

<![CDATA[Important text I need]]>

<![CDATA[imageName.jpg]]>

或类似的东西。

结果应该是一个数组,在第一个例子中,数组的内容是 “我需要的重要文字”, "imageName.jpg", “仍然重要的文字”

另一个结果将是一个数组,其中一个对象包含图像名称或文本。

我现在被这个问题困扰了一段时间,因为我不太擅长正则表达式。 有没有人遇到过同样的问题,你是怎么解决的?

或者有没有我错过的简单解决方法?

提前致谢!

【问题讨论】:

  • 如果你在 cdata 中只有 html,你可以选择 NSXMLParser

标签: objective-c image parsing text cdata


【解决方案1】:

如果您使用NSXMLParser,则有一个名为foundCDATA 的委托方法,如下所示:

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{
    if (!parseElement) {
        return;
    }
    if (parsedElementData==nil) {
        parsedElementData = [[NSMutableData alloc] init];
    }
    [parsedElementData appendData:CDATABlock];

    //Grabs the whole content in CDATABlock.
    NSMutableString *content = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];

 }

现在将this prewritten class 添加到您的项目中。然后将其导入到您要使用的解析器类中:

#import NSString_stripHTML

现在您只需将以下行添加到foundCDATAmethod:

NSString *strippedContent;
strippedContent = [content strippedHtml];

现在您将获得没有任何额外字符的剥离文本。你可以从这个剥离的文本中添加任何你想要的字符串。

【讨论】:

    【解决方案2】:

    所以我找到了自己的解决方案: 第一种方法在 cdataString 中搜索任何 HTML。 如果 cdataString 包含任何 HTML,我会搜索“src=...”的出现。

    - (NSString *)stringByStrippingHTML:(NSString *)htmlString {
        NSRange r;
        while ((r = [htmlString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound){
            // substring from htmlString starting with "<" and ends with ">"
            NSString *substring     = [htmlString substringWithRange:r];
    
            //new Image String, stays empty if no image is found
            NSString *imageString   = @"";
    
            //length >= 9 because shortest possible result can be length nine, i.e. "src=1.png"
            if (substring.length >= 9) {
    
                //substring contains String "src=" ?
                NSRange imageRange      = [substring rangeOfString:@"src=[^>]+" options:NSRegularExpressionSearch];
                if (imageRange.location != NSNotFound) {
    
                    //find the image name
                    imageString  = [self imageFromHTMLString:substring];
                }
                //set the image string the imagename + my seperator tag
                imageString = [NSString stringWithFormat:@"##__##%@##__##",imageString];
            }
            //replace html stuff with either emty string or my imagename
            htmlString = [htmlString stringByReplacingCharactersInRange:r withString:imageString];
    
        }
        return htmlString;
    }
    - (NSString *)imageFromHTMLString:(NSString *)htmlString{
        NSRange range;
    
        NSString *result = @"";
        while ((range = [htmlString rangeOfString:@"src=[^>]+ " options:NSRegularExpressionSearch]).location != NSNotFound) {
    
            htmlString  = [[[htmlString substringWithRange:range] componentsSeparatedByString:@" "] objectAtIndex:0];
            result      = [htmlString stringByReplacingOccurrencesOfString:@"src=" withString:@""];
        }
    
        return result;
    
    }
    

    方法用于:

    myCdataString = [self stringByStrippingHTML:myCdataString];
    

    返回值是一个字符串,格式为:

    Important Text I need##__##ImageName.png##__##More ImportantText I need
    

    可以通过 componentsSeparatedByString:@"##__##" 来创建一个数组

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 1970-01-01
      相关资源
      最近更新 更多