I have a filelist like /Test/Demo1/Demo2/Demo3/demo.mp4 and want make XML document in Objective-C. I have a code for make NSMutableDictionary:
-(NSMutableDictionary *)addFile:(NSString *)path toTree:(NSMutableDictionary *)tree {
NSMutableDictionary *node = tree;
NSArray *components = [path componentsSeparatedByString:@"/"];
for (NSUInteger i = 0; i < [components count] - 1; i++) {
NSString *key = components[i];
if (node[key] == nil) {
node[key] = [NSMutableDictionary dictionary];
}
node = node[key];
}
NSString *name = [components lastObject];
if ([name length] > 0) {
node[name] = [NSMutableDictionary dictionary];
node = node[name];
}
return node;
}
But i have no good results. Advise me how to convert a filelist to an NSMutableArray or NSXMLDocument.
Thank you
There are some examples in the Tree-Based XML Programming Guide. Check "Creating a New XML Document" after "Creating a Document Object From Existing XML" and also "Writing Out an XML Document" that shows how to convert it to NSData (which can be written to file or converted to NSString).
One more article with examples.
This XML API is needed if you want to create formally conformant standard XML files.
Alternatively you can just manually build a string using NSMutableString. One example of that from the internet works with a different structure than yours, but you should be able to get the idea.
After you build your files and directories tree, you can traverse it using an algorithm like Depth-first search, and along the way build your NSXMLDocument or NSMutableString.