• 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: Nir Soffer <email@hidden>
  • Date: Sun, 19 Nov 2006 22:04:55 +0200


On Nov 19, 2006, at 15:22, Al Skipp wrote:

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);

Detach just detach the node from it parent. You can use:

NSXMLElement *element = [fragment rootElement];
[element detach];
[textFrame addChild:element];


I would add a category with a method to create element from xml string:

@interface NSXMLElement (CreatingFromXML)
+ (NSXMLElement *)elementWithXMLString:(NSString *)xmlString options: (unsigned int)mask error:(NSError **)error;
@end


@implementation NSXMLElement (CreatingFromXML)

+ (NSXMLElement *)elementWithXMLString:(NSString *)xmlString options: (unsigned int)mask error:(NSError **)error
{
NSXMLDocument *fragment = [[[NSXMLDocument alloc] initWithXMLString:xmlString
options:options error:error];
if (fragment == nil)
return nil;


	NSXMLElement *result = [fragment rootElement];
	[result detach];
	[fragment release];

	return result;
}

@end

Warnings: written in Mail, untested


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: Al Skipp <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>)
 >Re: NSXML - preserving white space in new elements (From: Al Skipp <email@hidden>)

  • Prev by Date: Re: Launch bundled application before main application
  • Next by Date: Re: 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