NSXML替换“<<与“%lt”相比, [英] NSXML replacing "<" with "%lt;"
问题描述
我看到有其他问题与此相关,但没有使用NSXML。
I see there are other questions related to this, but none using NSXML.
所以,我使用NSXML从头构建一个XML文档,一个带有字符串值的NSXMLNode,所有的<该字符串中的字符将替换为& lt;
So, I'm constructing an XML document from scratch using NSXML and when I create a NSXMLNode with a string value, all the "<" characters in that string are replaced with "& lt;" when I output the node, or save it to a file.
例如:
NSXMLNode *description = [NSXMLNode elementWithName:@"description"
stringValue:@"<![CDATA[Some Description</a>]]>"];
然后当我做
NSLog(@"description: %@", description);
我得到所有'<'字符替换为& lt;的节点。但是当我做
I get the node with all the '<' characters replaced with "& lt;". However when I do
NSLog(@"description string value: %@", [description stringValue]);
我得到正确的字符串输出。此XML文档将被保存为Google地球的KML文件,而Google地球在找到& lt;时会出现错误。令牌。任何想法如何使NSXML只输出'<'?我使用OSX 10.6和XCode 3.2 btw。
I get the correct string output. This XML document is going to be saved as a KML file for google earth, and google earth gives me an error when it finds the "& lt;" token. Any idea how to make NSXML just output the '<'? I'm using OSX 10.6 and XCode 3.2 btw.
推荐答案
有一个特殊的选项标志指示CDATA,诀窍是让可可为你写<![CDATA [
和]]
>
There's a special options flag for indicating CDATA that will help here. The trick is to let cocoa write the <![CDATA[
and ]]
bookends for you:
NSXMLNode *cdata = [[[NSXMLNode alloc] initWithKind:NSXMLTextKind
options:NSXMLNodeIsCDATA] autorelease];
[cdata setStringValue:@"Some CDATA Description"];
NSXMLElement *description = [NSXMLNode elementWithName:@"description"];
[description addChild:cdata];
NSLog(@"description: %@", description);
// yields: <description><![CDATA[Some CDATA Description]]></description>
要获得带尖括号完整的字符串:
To obtain a string with the angle brackets intact:
NSString *output = [description XMLString];
这篇关于NSXML替换“<<与“%lt”相比,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!