开发者

Editing an NSXMLDocument without escaping special characters

开发者 https://www.devze.com 2023-04-03 07:02 出处:网络
I have an NSXMLDocument that I am editing and then saving out. The problem is, it\'s escaping my & and <.

I have an NSXMLDocument that I am editing and then saving out. The problem is, it's escaping my & and <.

NSArray *nodes = [document nodesForXPath:@"JDF/JDF/ResourcePool/od:Ticket/od:Parameter/DefaultOutputOption" error:ni开发者_如何学Gol];
[[nodes objectAtIndex:0] setStringValue:@"34&quot;"];
NSLog(@"%@", [[nodes objectAtIndex:0]stringValue]);
[[nodes objectAtIndex:1] setStringValue:@"1<2"];
NSLog(@"%@", [[nodes objectAtIndex:1]stringValue]);
NSData *xmlData = [document XMLDataWithOptions:NSXMLNodePreserveAll];
[xmlData writeToFile:path atomically:YES];

In console I'm getting what you'd expect from the NSLog statements:

2011-09-06 16:05:56.480 My App[14369:707] 34&quot;
2011-09-06 16:05:56.482 My App[14369:707] 1<2

But in my XML Document I get:

34&amp;quot;
1&lt;2

What am I doing wrong?


Technically you're not doing anything wrong. When you set the text value for a node, you should use the raw, unescaped form. When the nodes are serialized to XML, some characters will be escaped, e.g. " will become &quot; and < will become &lt;.

I guess what really want to enter as text in the first node is 34", which in XML serialized form is 34&quot;.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号