Re: Am I Reinventing the Wheel? (Part I)
Re: Am I Reinventing the Wheel? (Part I)
- Subject: Re: Am I Reinventing the Wheel? (Part I)
- From: Keary Suska <email@hidden>
- Date: Thu, 08 Jan 2015 09:08:28 -0700
On Jan 8, 2015, at 5:43 AM, Charles Jenkins <email@hidden> 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 (email@hidden)
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:
This email sent to email@hidden