On Jan 8, 2015, at 5:43 AM, Charles Jenkins <cejw...@gmail.com> wrote:

> I need to deal with two issues that are probably already handled in some 
> Cocoa API I just haven't found yet. This email asks about the first of these 
> issues.  
> 
> I'm writing data to XML. When you create a node and set its string contents, 
> the node will happily accept whatever string you give and allow you to 
> serialize information XML deserialization cannot then recreate. In my case, 
> the string in question contained curled quotes. I could serialize and save 
> the data—and if I remember correctly* the output looked good when I inspected 
> the file on disk—but reading it back and deserializing it led to disaster! 
> Right now I'm using NSString stringByAddingPercentEncoding: and having no 
> further problems with curled quotes, but I'm sure that's a poor long-term 
> solution.
> 
> *I encountered this problem a few weeks ago and put off a final solution by 
> using the percent encoding.
> 
> Is there already a Cocoa API call that would convert a string to use HTML 
> entities so I could safely put any string into an XML node?

You can apparently route through NSAttributedString (found via StackOverflow):

NSAttributedString *s = [[NSAttributedString alloc] 
initWithString:sourceString];
NSDictionary *documentAttributes = @{NSDocumentTypeDocumentAttribute: 
NSHTMLTextDocumentType};
NSData *htmlData = [s dataFromRange:NSMakeRange(0, s.length) 
documentAttributes:documentAttributes error:NULL];
NSString *htmlString = [[NSString alloc] initWithData:htmlData 
encoding:NSUTF8StringEncoding];

HTH,

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to