Objective c 如何使用XML编写CDATA?
我正在为iOS创建一个需要创建XML文档的应用程序。我是通过KissXML实现的。XML的一部分看起来像Objective c 如何使用XML编写CDATA?,objective-c,ios,kissxml,Objective C,Ios,Kissxml,我正在为iOS创建一个需要创建XML文档的应用程序。我是通过KissXML实现的。XML的一部分看起来像 <ISIN><![CDATA[12345678]]></ISIN> 我在KissXML中找不到任何选项来创建CDATA部分。简单地添加一个包含CDATA内容的字符串作为文本将导致转义特殊字符,如。有谁能给我一个关于如何使用KissXML编写CDATA的提示吗?我自己找到了一个解决方法-基本上是将CDATA伪装成一个新的XML文档。一些有效的代码: +
<ISIN><![CDATA[12345678]]></ISIN>
我在KissXML中找不到任何选项来创建CDATA部分。简单地添加一个包含CDATA内容的字符串作为文本将导致转义特殊字符,如<和>。有谁能给我一个关于如何使用KissXML编写CDATA的提示吗?我自己找到了一个解决方法-基本上是将CDATA伪装成一个新的XML文档。一些有效的代码:
+(DDXMLElement* ) createCDataNode:(NSString*)name value:(NSString*)val {
NSMutableString* newVal = [[NSMutableString alloc] init];
[newVal appendString:@"<"];
[newVal appendString:name];
[newVal appendString:@">"];
[newVal appendString:@"<![CDATA["];
[newVal appendString:val];
[newVal appendString:@"]]>"];
[newVal appendString:@"</"];
[newVal appendString:name];
[newVal appendString:@">"];
DDXMLDocument* xmlDoc = [[DDXMLDocument alloc] initWithXMLString:newVal options:DDXMLDocumentXMLKind error:nil];
return [[xmlDoc rootElement] copy];
}
+(ddxmlement*)createCDATA节点:(NSString*)名称值:(NSString*)val{
NSMutableString*newVal=[[NSMutableString alloc]init];
[newVal appendString:@”“];
[newVal appendString:@”“];
[newVal appendString:@”“];
DDXMLDocument*xmlDoc=[[DDXMLDocument alloc]initWithXMLString:newVal选项:DDXMLDocumentXMLKind错误:nil];
返回[[xmlDoc rootElement]copy];
}
天哪!这只是我认为是一个“肮脏的黑客”。它能起作用,但感觉不对劲。如果能找到一个“好”的解决方案,我将不胜感激。即使它很难看,也能奏效。我已经清理了字符串创建代码并将其添加到一个类别中
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.h”
@接口DDXMLNode(CDATA)
/**
创建具有内部CDATA块的新XML元素
*/
+(id)CDATA元素WithName:(NSString*)name stringValue:(NSString*)字符串;
@结束
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
#导入“DDXMLNode+CDATA.h”
#导入“ddxmlement.h”
#导入“DDXMLDocument.h”
@实现DDXMLNode(CDATA)
+(id)CDATA元素WithName:(NSString*)name stringValue:(NSString*)string
{
NSString*nodeString=[NSString stringWithFormat:@',名称,字符串,名称];
DDXMLElement*CDATA节点=[[DDXMLDocument alloc]initWithXMLString:nodeString
选项:DDXMLDocumentXMLKind
错误:nil]。根元素;
返回[CDATA节点副本];
}
@结束
该代码也可在本文档中找到