2

I am reading a xml and finaly just need to remove the CDATA Infos in my results

For example: I get:

 "<![CDATA[iPhone 4-Rückgaberecht: Deutsche Telekom kulant]]>"

just need "iPhone 4-Rückgaberecht: Deutsche Telekom kulant"

thx chris

Edit to your answers: I am not using NSXMLParser (thats the reason I make my own parser)

Found some suggestions with:

- (NSString *)stringByDecodingXMLEntities;
but dont know how to implement. I always get
> YourController may not respond to '-stringByDecodingXMLEntities" <
christian Muller
  • 5,016
  • 6
  • 34
  • 44
  • I don't know anything about the iPhone, but the XML parser **should** remove the CDATA sections for you. Shame on him/her/it if he/she/it doesn't. – MvanGeest Jul 29 '10 at 09:23
  • I'd seriously recommend using NSXMLParser. XML is much more complicated than most people think. Rolling your own parser is a recipe for bugs and incompatibilities. – bobince Jul 29 '10 at 10:04

4 Answers4

2

Ok, i solved it with that:

 NSMutableString* resultString;


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)s {
   resultString = [[NSMutableString alloc] init];
    [resultString appendString:s];
}

- (NSString*)convertEntiesInString:(NSString*)s {
    if(s == nil) {
    NSLog(@"ERROR : Parameter string is nil");
    }
    NSString* xmlStr = [NSString stringWithFormat:@"<d>%@</d>", s];
    NSData *data = [xmlStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSXMLParser* xmlParse = [[NSXMLParser alloc] initWithData:data];
    [xmlParse setDelegate:self];
    [xmlParse parse];
    NSString* returnStr = [[NSString alloc] initWithFormat:@"%@",resultString];
  return returnStr;
}

call: myConvertedString = [self convertEntiesInString:myOriginalString];

christian Muller
  • 5,016
  • 6
  • 34
  • 44
1

use

  • (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock

method instead of

  • (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

thats it

Ganesh
  • 1,059
  • 1
  • 10
  • 28
1

you could try a regex

replace <!\[CDATA\[(.*)\]\]> with $1

opatut
  • 6,708
  • 5
  • 32
  • 37
  • let me know how to to in objc-C :) – christian Muller Jul 29 '10 at 09:34
  • sry, don't know anything about obj-c. Seems like google doesnt find anything either. http://stackoverflow.com/questions/422138/regular-expressions-in-an-objective-c-cocoa-application look at the accepted answer... – opatut Jul 29 '10 at 09:50
0

If you already have the String in String format with the you can remove it like so:

//Declare what you wish to remove
NSString * suffixTorRemove = @"<![CDATA[";
NSString * prefixToRemove = @"]]>";

//Now create a new string which uses your existing string and removes the declared occurrences above
NSString * newString = [yourString stringByReplacingOccurrencesOfString:suffixTorRemove withString:@""];
//Now the first part has changed, time to remove the second part
NSString * newString2 = [newString stringByReplacingOccurrencesOfString:prefixTorRemove withString:@""];

Quick and simple :-)

App Dev Guy
  • 5,396
  • 4
  • 31
  • 54