Skip to content

Instantly share code, notes, and snippets.

@krhoyt
Last active December 27, 2015 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krhoyt/7359128 to your computer and use it in GitHub Desktop.
Save krhoyt/7359128 to your computer and use it in GitHub Desktop.
Create an XML document and write it to disk.
- ( void ) createAndWriteXMLDocument : ( NSString * ) fileName
{
NSArray * directories;
NSData * output;
NSFileManager * manager;
NSMutableString * path;
NSXMLDocument * doc;
NSXMLElement * root;
NSXMLNode * first;
NSXMLNode * last;
root = ( NSXMLElement * )[NSXMLNode elementWithName : @"person"];
doc = [[NSXMLDocument alloc] initWithRootElement : root];
[doc setVersion:@"1.0"];
[doc setCharacterEncoding:@"UTF-8"];
first = [[NSXMLNode alloc] initWithKind : NSXMLElementKind];
[first setName : @"first"];
[first setObjectValue : @"Kevin"];
[root addChild : first];
last = [[NSXMLNode alloc] initWithKind : NSXMLElementKind];
[last setName : @"last"];
[last setObjectValue : @"Hoyt"];
[root addChild : last];
manager = [NSFileManager defaultManager];
directories = NSSearchPathForDirectoriesInDomains( NSDesktopDirectory, NSUserDomainMask, YES );
path = [[NSMutableString alloc] initWithString : [directories objectAtIndex : 0]];
[path appendString: @"/"];
[path appendString: fileName];
output = [doc XMLDataWithOptions : NSXMLNodePrettyPrint];
[output writeToFile: path atomically:YES];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment