• 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 - preserving white space in new elements
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSXML - preserving white space in new elements


  • Subject: Re: NSXML - preserving white space in new elements
  • From: Al Skipp <email@hidden>
  • Date: Sun, 19 Nov 2006 13:22:54 +0000

Thank you both for the responses,
It was an interesting solution to create a temporary NSXMLDocument then extract the root element from it.
When I tried adding the root element I was given an error, stating that I had to either copy or detach the node before adding it as a child to another element. I went with the copy approach because I couldn't fathom how to use the detach method; it doesn't return anything. Perhaps I'm missing something obvious, but how do you keep a reference to a node that you detach? It seems equivalent to delete.
Anyway, here is the code I have working. Does it seem like a reasonable solution?


NSString *xmlString =@"<paragraph>The <bold>cat</bold> <italic>sat</ italic> on the <bold>mat</bold></paragraph>";

NSXMLElement *textFrame = [[[NSXMLElement alloc] initWithName:@"textFrame"]autorelease];
NSXMLDocument *fragment = [[[NSXMLDocument alloc] initWithXMLString:xmlString options:NSXMLNodePreserveWhitespace error:nil]autorelease];


NSXMLElement *paragraph =[[fragment rootElement]copy];
[textFrame addChild:paragraph];
[paragraph release];

NSLog(@"%@", textFrame);



On 19 Nov 2006, at 10:19, Nir Soffer wrote:


On Nov 19, 2006, at 11:38, Pascal Pochet wrote:


Le 18-nov.-06 à 16:41, Al Skipp a écrit :

I'm trying to create a simple XML document which holds information regarding text styling, so it is important to maintain white space between words, but I'm having difficulty achieving this. Here's an example.
I'd like to form this string into an XML element:


NSString *xmlString =@"<paragraph>The <bold>cat</bold> <italic>sat</italic> on the <bold>mat</bold></paragraph>";

if I try this:

NSXMLElement *xml = [[[NSXMLElement alloc] initWithXMLString:xmlString error:&err]autorelease];

then I lose the spacing between 'cat' and 'sat'.

I can use:

NSXMLDocument *xml = [[[NSXMLDocument alloc] initWithXMLString:xmlString options:NSXMLNodePreserveWhitespace error:&err]autorelease];

which preserves spacing, though I don't want a new NSXMLDocument, I need a new element to add to an existing document.

Is there a way of creating an NSXMLElement with 'options:NSXMLNodePreserveWhitespace'?

I've attempted using:
NSXMLElement *xml = [[[NSXMLElement alloc] initWithKind:NSXMLElementKind options:NSXMLNodePreserveAll] autorelease];


and then setting the name and string. However, the XML in the string is then escaped as thus: &lt;bold>cat&lt;/bold>


Your problem has nothing to do with Cocoa…

XML-in-XML is a classic question in XML forums…

- escaping <, > as entities,
- using CDATA, e.g. @"<![CDATA[<paragraph>The <bold>cat</bold> <italic>sat</italic> on the <bold>mat</bold></paragraph>]]>"
- encoding the XML-in-XML in base64
- using a related DTD with CDATA section definitions
- …


are among the classic solutions…

True, but maybe there is a simple Cocoa solution:

NSXMLDocument *fragment = [[[NSXMLDocument alloc] initWithXMLString:xmlString
options:NSXMLNodePreserveWhitespace error:&err] autorelease];


Then add [fragment rootElement] to your document.


Best Regards,

Nir Soffer


_______________________________________________ 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 - preserving white space in new elements
      • From: Nir Soffer <email@hidden>
References: 
 >NSXML - preserving white space in new elements (From: Al Skipp <email@hidden>)
 >Re: NSXML - preserving white space in new elements (From: Pascal Pochet <email@hidden>)
 >Re: NSXML - preserving white space in new elements (From: Nir Soffer <email@hidden>)

  • Prev by Date: Re: Confused about bindings
  • Next by Date: IOPMSchedulePowerEvent - laptop lid closed
  • Previous by thread: Re: NSXML - preserving white space in new elements
  • Next by thread: Re: NSXML - preserving white space in new elements
  • Index(es):
    • Date
    • Thread