Re: NSXML and Character References
Re: NSXML and Character References
- Subject: Re: NSXML and Character References
- From: James Housley <email@hidden>
- Date: Mon, 21 Nov 2005 10:50:05 -0500
On Nov 21, 2005, at 10:26 AM, Juan Alvarez wrote:
Hi,
I am creating an NSXMLDocument and adding the children as
NSXMLNodes. The problem I am having is that the xml being written
out escapes only "&" characters and not apostrophes or quotations.
Why is that? Is there a way to have all the characters escape
properly. I have already tried to replace the characters with the
appropriate escaping, but for some reason the xml still wants to
escape the & character that are in there. Please help. I have
provided sample code below and the ouput:
Code:
NSXMLElement *root = (NSXMLElement *)[NSXMLNode
elementWithName:@"rss"];
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc]
initWithRootElement:rootElement];
[xmlDoc setVersion:@"1.0"];
[xmlDoc setCharacterEncoding:@"UTF-8"];
NSXMLNode *node = [NSXMLNode elementWithName:@"title"];
[node setStringValue:@"My \"Mom\" & \"Dad's\" Show"];
[root addChild:node];
NSData *data = [xmlDoc XMLDataWithOptions:NSXMLNodePrettyPrint];
[data writeToFile:filepath atomically:YES];
Output:
<?xml version="1.0" encoding="UTF-8"?>
<rss>
<title>My "Mom" & "Dad's" Show</title>
</rss>
&, < and > are required to always be escaped by the spec.
Quotations and apostrophes only are needed in certain instances. In
your above example there is no need to escape them and are valid like
that. However, they must be escaped in the case below.
<?xml version="1.0" encoding="UTF-8"?>
<rss>
<title>My "Mom" & "Dad's" Show</title>
<tag2 attribute="My "Mom" & "Dad's"
Show">I is a good show</tag2>
<tag3 attribute='My "Mom" & "Dad's" Show'>I is a good
show</tag3>
</rss>
From http://www.w3.org/TR/2004/REC-xml-20040204/#syntax
To allow attribute values to contain both single and double quotes,
the apostrophe or single-quote character (') may be represented as
"'", and the double-quote character (") as """.
Jim
--
/"\ ASCII Ribbon Campaign .
\ / - NO HTML/RTF in e-mail .
X - NO Word docs in e-mail .
/ \ -----------------------------------------------------------------
email@hidden http://www.FreeBSD.org The Power to Serve
email@hidden http://www.TheHousleys.net
---------------------------------------------------------------------
Do not meddle in the affairs of dragons, for you are crunchy and taste
good with ketchup.
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden