Re: How to add a DOCTYPE declaration to an NSXMLDocument?
Re: How to add a DOCTYPE declaration to an NSXMLDocument?
- Subject: Re: How to add a DOCTYPE declaration to an NSXMLDocument?
- From: Matt Neuburg <email@hidden>
- Date: Wed, 31 Mar 2010 08:42:10 -0700
- Thread-topic: How to add a DOCTYPE declaration to an NSXMLDocument?
On Wed, 31 Mar 2010 01:00:55 -0700, Bruce Sharpe <email@hidden>
said:
>> Sounds promising, but the document type declaration doesn't get written
>out to file.
>
>If I create the document using a string instead, it works:
>
>NSString *docString = [NSString stringWithFormat:@"<!DOCTYPE
>myDocType><myDocType></myDocType>"];
>NSXMLDocument *myDoc = [[NSXMLDocument alloc] initWithXMLString:docString
>options:0 error:&err];
>[[myDoc DTD] setName:@"myDocType"];
>
>Seems equivalent to half a dozen other things I tried, but this is the only
>one that works.
Your "success" here has nothing to do with your "using a string". Here's
proof. I ran your code above, and then immediately created another way of
generating and writing out *exactly* the same document:
NSXMLElement* e = [[NSXMLElement alloc] initWithName:@"myDocType"];
NSXMLDocument* d = [[NSXMLDocument alloc] initWithRootElement:e];
[d setVersion: @"1.0"];
[d setCharacterEncoding: @"UTF-8"];
NSXMLDTD* dtd = [[NSXMLDTD alloc] init];
[dtd setName: @"myDocType"];
[d setDTD:dtd];
NSData* output = [d XMLData];
NSString* f = [@"~/Desktop/output.xml" stringByExpandingTildeInPath];
[output writeToURL:[NSURL fileURLWithPath:f] atomically:YES];
// memory management omitted
Here are some suggestions for going forward:
* Read the documentation properly. For example, a class has a superclass,
which may contain the methods you're looking for (probably why setName: was
missed).
* Be rigorous with XML. For example, your previous note said that you "write
[the document] ... after adding several elements to it." But if your DTD
says <!DOCTYPE myDocType> and you then add elements to your document without
modifying that DTD, you've got invalid XML. This might affect NSXML's
behavior - not worth the risk.
* Use standard Cocoa. For example, line in your code is a little odd:
NSXMLDTD *myDTD = [[NSXMLNode alloc] initWithKind:NSXMLDTDKind];
Why declare an NSXMLDTD and alloc it as a different class? Perhaps harmless,
but again, not worth the risk.
* Test rigorously. For example, in your previous note, you got the "right"
answer in NSLog but the "wrong" answer when outputting to a file. That might
mean something about what you did after the NSLog call - could be worth
looking into.
m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
http://www.tidbits.com/matt/default.html#applescriptthings
_______________________________________________
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