문제

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