1. 程式人生 > >xml 對特殊字元的處理(新增cdata標籤)

xml 對特殊字元的處理(新增cdata標籤)

有時候會在xml裡新增包含類似'&'的特殊字元,這樣在進行XML解析時會導致錯誤解析得到不正確的資訊,故而有必要針對這些特殊字元進行處理.利用cdata標籤,這樣xml在解析時就不會錯誤判斷了.

附上原始碼:

//cdata
+ (id)cdataElementWithName:(NSString *)name stringValue:(NSString *)string
{
    NSString* nodeString = [NSString stringWithFormat:@"<%@><![CDATA[%@]]></%@>", name, string, name];
    DDXMLElement* cdataNode = [[DDXMLDocument alloc] initWithXMLString:nodeString
                                                               options:DDXMLDocumentXMLKind
                                                                 error:nil].rootElement;
    return [cdataNode copy];
}