Anton Simakov
Anton Simakov

Reputation: 303

Why NSXMLDocument doesn't escape newline character (\n)

I'm running this code:

NSXMLElement *element = [NSXMLElement elementWithName:@"name" stringValue:@"value\n"];
NSXMLDocument *document = [[NSXMLDocument alloc] initWithRootElement:element];
NSLog(@"%@", document.XMLString);

And I'm getting this result:

<name>value
</name>

But I'm expecting the result to be:

<name>value&#xA;</name>

Does anybody know why NSXMLDocument doesn't escape \n? Is it an expected behavior?

I also tried XMLStringWithOptions: with different options, e.g. NSXMLNodePreserveWhitespace, but with no luck.

Upvotes: 4

Views: 110

Answers (1)

slf
slf

Reputation: 22777

Because \n is valid xml. If you really want values to be URL encoded, you'll need to encode them.

How do I URL encode a string

Upvotes: 2

Related Questions