`
jsntghf
  • 浏览: 2480547 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

NSString的扩展类

    博客分类:
  • iOS
阅读更多

 NSString+XMLExtensions.h

 

#import <UIKit/UIKit.h>

@interface NSString(XMLExtensions) 

+ (NSString *) encodeXMLCharactersIn:(NSString *)source;
+ (NSString *) decodeXMLCharactersIn:(NSString *)source;

@end

 

NSString+XMLExtensions.m

 

#import "NSString+XMLExtensions.h"

@implementation NSString(XMLExtensions)

+ (NSString *)encodeXMLCharactersIn:(NSString *)source {
	if (![source isKindOfClass:[NSString class]] || !source)
		return @"";
	
	NSString *result = [NSString stringWithString:source];
	
	if ([result rangeOfString:@"&"].location != NSNotFound)
		result = [[result componentsSeparatedByString: @"&"] componentsJoinedByString: @"&amp;"];
	
	if ([result rangeOfString:@"<"].location != NSNotFound)
		result = [[result componentsSeparatedByString: @"<"] componentsJoinedByString: @"&lt;"];
	
	if ([result rangeOfString:@">"].location != NSNotFound)
		result = [[result componentsSeparatedByString: @">"] componentsJoinedByString: @"&gt;"];
	
	if ([result rangeOfString:@"\""].location != NSNotFound)
		result = [[result componentsSeparatedByString: @"\""] componentsJoinedByString: @"&quot;"];
	
	if ([result rangeOfString:@"'"].location != NSNotFound)
		result = [[result componentsSeparatedByString: @"'"] componentsJoinedByString: @"&apos;"];
	
	return result;
}

+ (NSString *) decodeXMLCharactersIn:(NSString *)source {	
	if (![source isKindOfClass:[NSString class]] || !source)
		return @"";
    
	NSString *result = [NSString stringWithString:source];
	
	if ([result rangeOfString:@"&amp;"].location != NSNotFound)
		result = [[result componentsSeparatedByString: @"&amp;"] componentsJoinedByString: @"&"];
	
	if ([result rangeOfString:@"&lt;"].location != NSNotFound)
		result = [[result componentsSeparatedByString: @"&lt;"] componentsJoinedByString: @"<"];
	
	if ([result rangeOfString:@"&gt;"].location != NSNotFound)
		result = [[result componentsSeparatedByString: @"&gt;"] componentsJoinedByString: @">"];
	
	if ([result rangeOfString:@"&quot;"].location != NSNotFound)
		result = [[result componentsSeparatedByString: @"&quot;"] componentsJoinedByString: @"\""];
	
	if ([result rangeOfString:@"&apos;"].location != NSNotFound)
		result = [[result componentsSeparatedByString: @"&apos;"] componentsJoinedByString: @"'"];
	
	return result;
}

@end

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics