• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSXML and Character References
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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" &amp; "Dad's" Show</title>
</rss>


&amp;, &lt; and &gt; 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" &amp; "Dad's" Show</title>
<tag2 attribute="My &quot;Mom&quot; &amp; &quot;Dad's&quot; Show">I is a good show</tag2>
<tag3 attribute='My "Mom" &amp; "Dad&apos;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 "&apos;", and the double-quote character (") as "&quot;".


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

  • Follow-Ups:
    • Re: NSXML and Character References
      • From: Juan Alvarez <email@hidden>
References: 
 >NSXML and Character References (From: Juan Alvarez <email@hidden>)

  • Prev by Date: on 2nd display no mouse allowed
  • Next by Date: Re: on 2nd display no mouse allowed
  • Previous by thread: NSXML and Character References
  • Next by thread: Re: NSXML and Character References
  • Index(es):
    • Date
    • Thread