您的位置:首页 > 其它

去除透传,转义字符

2015-12-19 22:16 260 查看
NSXMLElement  stringValue 的妙用   想了很多方法去除<![CDATA[  ]]>无果,突然发现stringValue管用

 NSXMLElement *msgXml = [[DDXMLElement
alloc]
initWithXMLString:message.stringValue
error:nil];//message.stringValue已经解析好了转义的符号

        //IM平台的msg_content节点
       
NSXMLElement *immsg_contentXml = [msgXml
elementForName:@"msg_content"];

       //PA平台的msg_content节点
       
NSXMLElement *pamsg_contentXml = [[DDXMLElement
alloc] initWithXMLString:immsg_contentXml.stringValue
error:nil];//immsg_contentXml.stringValue已经解析去掉了“<![CDATA[ 
]]>”

        
       
int mediaType = [[[pamsg_contentXml
elementForName:@"media_type"]
stringValue]intValue];

另外

//XML转义特殊字符
- (NSString *)stringByEncodingXMLEntity
{
   
NSString *tempStr;

    tempStr = [self
stringByReplacingOccurrencesOfString:@"\""
withString:@"""];

    tempStr = [tempStr stringByReplacingOccurrencesOfString:@"'"
withString:@"'"];

    tempStr = [tempStr stringByReplacingOccurrencesOfString:@"&"
withString:@"&"];

    tempStr = [tempStr stringByReplacingOccurrencesOfString:@"<"
withString:@"<"];

    tempStr = [tempStr stringByReplacingOccurrencesOfString:@">"
withString:@">"];

    
   
return tempStr;
}

//XML还原特殊字符
- (NSString *)stringByDecodingXMLEntity
{
   
NSString *tempStr;

    tempStr = [self
stringByReplacingOccurrencesOfString:@"""
withString:@"\""];

    tempStr = [tempStr stringByReplacingOccurrencesOfString:@"'"
withString:@"'"];

    tempStr = [tempStr stringByReplacingOccurrencesOfString:@"&"
withString:@"&"];

    tempStr = [tempStr stringByReplacingOccurrencesOfString:@"<"
withString:@"<"];

    tempStr = [tempStr stringByReplacingOccurrencesOfString:@">"
withString:@">"];

    
   
return tempStr;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  经验