我正在为需要创建 XML 文档的 iOS 创建应用程序。我通过 KissXML 执行此操作。部分 XML 看起来像
<ISIN><![CDATA[12345678]]></ISIN>
我在 KissXML 中找不到任何选项来创建 CDATA 部分。简单地添加一个带有 CDATA 内容的字符串作为文本将导致转义特殊字符,如 < 和 >。谁能给我一些关于如何使用 KissXML 编写 CDATA 的提示?
最佳答案
尽管the solution by @moq是丑陋的,它的工作原理。我已经清理了字符串创建代码并将其添加到一个类别中。
DDXMLNode+CDATA.h:
#import <Foundation/Foundation.h>
#import "DDXMLNode.h"
@interface DDXMLNode (CDATA)
/**
Creates a new XML element with an inner CDATA block
<name><![CDATA[string]]></name>
*/
+ (id)cdataElementWithName:(NSString *)name stringValue:(NSString *)string;
@end
DDXMLNode+CDATA.m:
#import "DDXMLNode+CDATA.h"
#import "DDXMLElement.h"
#import "DDXMLDocument.h"
@implementation DDXMLNode (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];
}
@end
此代码也可在 gist 中找到.
关于objective-c - 如何用 KissXML 编写 CDATA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11773890/