IOS read-write XML file

Source: Internet
Author: User

Write the file, pass in the information to be saved *********//Save the XML

-(void) SaveXML: (NSString *) data

{

Creating a File Manager

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

Get path

Parameter nsdocumentdirectory to get that path

Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);

NSLog (@ "paths=%@", paths);

NSString *documentsdirectory = [Paths path required for objectatindex:0];//destination

NSLog (@ "documentsdirectory=%@", documentsdirectory);

Change to the directory you want to operate

NSLog (@ "[Documentsdirectory stringbyexpandingtildeinpath]=%@", [Documentsdirectory Stringbyexpandingtildeinpath]) ;

[FileManager changecurrentdirectorypath:[documentsdirectory Stringbyexpandingtildeinpath];

Create file FileName File name, contents file contents, if Start no content can be set to Nil,attributes file properties, initially nil

Get file path

[FileManager removeitematpath:@ "username" Error:nil];

NSString *path = [documentsdirectory stringbyappendingpathcomponent:@ "Cc.txt"];

NSLog (@ "path=%@", Path);

Create a data buffer

Nsmutabledata *writer = [[Nsmutabledata alloc] init];

Add a string to the buffer

NSLog (@ "[File datausingencoding:nsutf8stringencoding]=%@", [File Data usingencoding:nsutf8stringencoding]);

[writer Appenddata:[data datausingencoding:nsutf8stringencoding]];

Add additional data to the buffer

Write buffered data to a file

[Writer Writetofile:path Atomically:yes];

}

/****** file Read ******/

-(NSString *) readXml

{

Creating a File Manager

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

Get path

Parameter nsdocumentdirectory to get that path

Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);

NSString *documentsdirectory = [Paths path required for objectatindex:0];//destination

Change to the directory you want to operate

[FileManager changecurrentdirectorypath:[documentsdirectory Stringbyexpandingtildeinpath];

Get file path filename is cc.txt

NSString *path = [documentsdirectory stringbyappendingpathcomponent:@ "Cc.txt"];

NSData *reader = [NSData Datawithcontentsoffile:path];

return [[NSString alloc] Initwithdata:reader

Encoding:nsutf8stringencoding];

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.