2013-02-14 3 views
0

그래서 CDATA 블록의 데이터를 구문 분석해야했습니다.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]]> 

또는이 같은

.

결과는 배열의 내용이 "내가 필요로하는 중요한 텍스트"될 첫 번째 예제의 CAS의 배열, "imageName.jpg" "아직 중요한 텍스트"

해야한다 다른 객체의 결과는 imageName 또는 텍스트를 포함하는 하나의 객체가있는 배열입니다.

저는 정규 표현식이 정말 좋지 않기 때문에 잠시 동안이 문제에 봉착했습니다. 여기 아무도 똑같은 문제를 겪었고 어떻게 해결 했습니까?

또는 아직 놓친 쉬운 해결 방법이 있습니까?

미리 감사드립니다.

+0

cdata에 html 만있는 경우 NSXMLParser를 사용할 수 있습니다. –

답변

0

그래서 내 해결책을 찾았습니다. 첫 번째 방법은 cdataString에서 HTML을 검색합니다. cdataString에 HTML이 포함되어 있으면 "src = ..."의 출현을 검색합니다.

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

배열을 만들기 componentsSeparatedByString를 통해 수행 할 수 있습니다 :

myCdataString = [self stringByStrippingHTML:myCdataString]; 

반환 값은 형식의 문자열입니다

- (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; 

} 

방법

함께 사용되는 @ "## __ ## "

1

NSXMLParser을 사용하는 경우라는 대리자 방법이 있습니다. 10은 다음과 같습니다.

이제 프로젝트에 this prewritten class을 추가하십시오.

이제
NSString *strippedContent; 
strippedContent = [content strippedHtml]; 

당신이 제거 텍스트없이있을 것이다 : 이제 단순히 foundCDATA 방법에 다음 행을 추가 할 수 있습니다

#import NSString_stripHTML

: 그럼 당신은 그것을 사용할 파서 클래스로 가져올 여분의 문자. 이 제거 된 텍스트에서 원하는 부분을 문자열로 지정할 수 있습니다.

관련 문제