Encode NSString for XML/HTML
There isn't an NSString method that does that. You'll have to write your own function that does string replacements. It is sufficient to do the following replacements:
- '&' => "&"
- '"' => """
- '\'' => "'"
- '>' => ">"
- '<' => "<"
Something like this should do (haven't tried):
[[[[[myStr stringByReplacingOccurrencesOfString: @"&" withString: @"&"]
stringByReplacingOccurrencesOfString: @"\"" withString: @"""]
stringByReplacingOccurrencesOfString: @"'" withString: @"'"]
stringByReplacingOccurrencesOfString: @">" withString: @">"]
stringByReplacingOccurrencesOfString: @"<" withString: @"<"];
Encode NSString for XML/HTML
There isn't an NSString method that does that. You'll have to write your own function that does string replacements. It is sufficient to do the following replacements:
- '&' => "&"
- '"' => """
- '\'' => "'"
- '>' => ">"
- '<' => "<"
Something like this should do (haven't tried):
[[[[[myStr stringByReplacingOccurrencesOfString: @"&" withString: @"&"]
stringByReplacingOccurrencesOfString: @"\"" withString: @"""]
stringByReplacingOccurrencesOfString: @"'" withString: @"'"]
stringByReplacingOccurrencesOfString: @">" withString: @">"]
stringByReplacingOccurrencesOfString: @"<" withString: @"<"];
How to convert string to xml in iOS? (objective C, xcode 7.2)
Simple code for SOAP request and parsing.
Goto the link: https://yuvarajmanickam.wordpress.com/2012/10/17/soap-message-request-and-xmlparsing-in-ios-app/
or
github.com/nicklockwood/XMLDictionary
or
stackoverflow.com/questions/803676/encode-nsstring-for-xml-html
URL-encoding and HTML-encoding NSStrings
Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.
- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
and
Returns a new string made by replacing in the receiver all percent escapes with the matching characters as determined by a given encoding.
- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Objective C HTML escape/unescape
This link contains the solution below. Cocoa CF has the CFXMLCreateStringByUnescapingEntities function but that's not available on the iPhone.
@interface MREntitiesConverter : NSObject <NSXMLParserDelegate>{
NSMutableString* resultString;
}
@property (nonatomic, retain) NSMutableString* resultString;
- (NSString*)convertEntitiesInString:(NSString*)s;
@end
@implementation MREntitiesConverter
@synthesize resultString;
- (id)init
{
if([super init]) {
resultString = [[NSMutableString alloc] init];
}
return self;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)s {
[self.resultString appendString:s];
}
- (NSString*)convertEntitiesInString:(NSString*)s {
if (!s) {
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] autorelease];
[xmlParse setDelegate:self];
[xmlParse parse];
return [NSString stringWithFormat:@"%@",resultString];
}
- (void)dealloc {
[resultString release];
[super dealloc];
}
@end
iOS HTML Unicode to NSString?
It's pretty easy to write your own HTML entity decoder. Just scan the string looking for &, read up to the following ;, then interpret the results. If it's "amp", "lt", "gt", or "quot", replace it with the relevant character. If it starts with #, it's a numeric entity. If the # is followed by an "x", treat the rest as hexadecimal, otherwise as decimal. Read the number, and then insert the character into your string (if you're writing to an NSMutableString
you can use [str appendFormat:@"%C", thechar]
. NSScanner
can make the string scanning pretty easy, especially since it already knows how to read hex numbers.
I just whipped up a function that should do this for you. Note, I haven't actually tested this, so you should run it through its paces:
- (NSString *)stringByDecodingHTMLEntitiesInString:(NSString *)input {
NSMutableString *results = [NSMutableString string];
NSScanner *scanner = [NSScanner scannerWithString:input];
[scanner setCharactersToBeSkipped:nil];
while (![scanner isAtEnd]) {
NSString *temp;
if ([scanner scanUpToString:@"&" intoString:&temp]) {
[results appendString:temp];
}
if ([scanner scanString:@"&" intoString:NULL]) {
BOOL valid = YES;
unsigned c = 0;
NSUInteger savedLocation = [scanner scanLocation];
if ([scanner scanString:@"#" intoString:NULL]) {
// it's a numeric entity
if ([scanner scanString:@"x" intoString:NULL]) {
// hexadecimal
unsigned int value;
if ([scanner scanHexInt:&value]) {
c = value;
} else {
valid = NO;
}
} else {
// decimal
int value;
if ([scanner scanInt:&value] && value >= 0) {
c = value;
} else {
valid = NO;
}
}
if (![scanner scanString:@";" intoString:NULL]) {
// not ;-terminated, bail out and emit the whole entity
valid = NO;
}
} else {
if (![scanner scanUpToString:@";" intoString:&temp]) {
// &; is not a valid entity
valid = NO;
} else if (![scanner scanString:@";" intoString:NULL]) {
// there was no trailing ;
valid = NO;
} else if ([temp isEqualToString:@"amp"]) {
c = '&';
} else if ([temp isEqualToString:@"quot"]) {
c = '"';
} else if ([temp isEqualToString:@"lt"]) {
c = '<';
} else if ([temp isEqualToString:@"gt"]) {
c = '>';
} else {
// unknown entity
valid = NO;
}
}
if (!valid) {
// we errored, just emit the whole thing raw
[results appendString:[input substringWithRange:NSMakeRange(savedLocation, [scanner scanLocation]-savedLocation)]];
} else {
[results appendFormat:@"%C", c];
}
}
}
return results;
}
iOS decode HTML entity to string
Both link below is working.
It is actually my careless mistake for missing out an character.
https://github.com/Koolistov/NSString-HTML
https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString%2BHTML.m
Related Topics
In Swift How to Call Method with Parameters on Gcd Main Thread
Save Images with Phimagemanager to Custom Album
Error: Initializer for Conditional Binding Must Have Optional Type, Not 'String'
Differences Between Udid and Uuid
Swift Countelements() Return Incorrect Value When Count Flag Emoji
Storing Asynchronous Cloud Firestore Query Results in Swift
Uitableviewcell with Autolayout Left Margin Different on iPhone and iPad
Get MAC Address of Bluetooth Low Energy Peripheral in iOS
Document Directory Path of Xcode Device Simulator
How to Put an Image in a Realm Database
Push Notification Not Receiving in Background iOS
How to Stop Multiple Times Method Calling of Didupdatelocations() in iOS
How to Fill a Uibezierpath with a Gradient
How to Put the Image on the Right Side of the Text in a Uibutton