2010-07-29 9 views
2

Je lis un xml et finaly juste besoin de supprimer la CDATA Infos dans mes résultatsiPhone supprimer <[CDATA

Par exemple: je reçois:

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

juste besoin « iPhone 4 Rückgaberecht : Deutsche Telekom kulant »

thx chris

Modifier vos réponses: Je n'utilise pas NSXMLParser (thats la raison pour laquelle je fais mon propre analyseur)

trouvé quelques suggestions avec:

- (NSString *)stringByDecodingXMLEntities; 
but dont know how to implement. I always get 
> YourController may not respond to '-stringByDecodingXMLEntities" < 
+0

je ne sais pas quoi que ce soit sur l'iPhone, mais le XML parser ** devrait ** supprimer les sections CDATA pour vous. Honte à lui/elle s'il/elle/elle ne le fait pas. – MvanGeest

+0

Je recommande sérieusement d'utiliser NSXMLParser. XML est beaucoup plus compliqué que la plupart des gens pensent. Rouler votre propre analyseur est une recette pour les bugs et les incompatibilités. – bobince

Répondre

2

Ok , je l'ai résolu avec cela:

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

appel: myConvertedString = [self convertEntiesInString: myOriginalString];

+0

Cela fonctionne bien pour moi. Merci – mkc842

+0

Toujours très bien. Merci. –

1

utilisation

  • (void) analyseur: (NSXMLParser *) foundCDATA analyseur : (NSData *) CDATABlock
procédé

au lieu de

  • (void) analyseur: (NSXMLParser *) analyseur foundCharacters: (NSString *) chaîne

thats it

+0

s'il vous plaît voir mon edit ci-dessus –

1

vous pouvez essayer un regex

remplacer <!\[CDATA\[(.*)\]\]> avec $1

+0

laissez-moi savoir comment à objc-C :) –

+0

sry, ne sais rien à propos d'obj-c. On dirait que google ne trouve rien non plus. http://stackoverflow.com/questions/422138/regular-expressions-in-an-objective-c-cocoa-application regardez la réponse acceptée ... – opatut

0

Si vous avez déjà la chaîne au format chaîne avec le vous pouvez le supprimer comme ceci:

//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:@""]; 

:-) rapide et simple