Вопрос

I am trying to copy the content of an XML file after selection from NSOpenPanel in cocoa. So lets say this XML file could be any where on desktop or other directories. Once I get the file Path, I am trying to read/copy the content to an NSMutableData like following.

I have filePath:

NSString* filePath = [files objectAtIndex:0];

e.g.: file://localhost/Users/Me/Documents/XMLFiles/FileA.xml

Trying to get the content and copy to an NSMutableData

xmlData  = [[NSMutableData alloc] initWithContentsOfFile:filePath];

or

xmlData  = [[NSMutableData alloc] initWithContentsOfURL:[NSURL URLWithString:filePath]];
Это было полезно?

Решение

If you truly want the data under NSMutableData form then the following should help you:

NSMutableData *theData = [NSMutableData dataWithContentsOfURL:[[theOpenPanel URLs] objectAtIndex:0]];

But I don't think that would be very useful. Why have xml as raw data? Mutable at that!!

Are you looking for the string so you can manually look at the text document?! Then perhaps this is what you're looking for : NSString's stringWithContentsOfFile:encoding:error

Perhaps you want to parse it afterwards?! Then I'd suggest reading on NSXMLParser.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top