简体   繁体   中英

How to save data into an XML file

My application contains 8 TextFields. When I click the submit button I want to save all the values from the TextFields into an XML file (and generate a XML file if required). How can I do this?

另一个可以完成工作的XML编写器是XSWI (无耻的插件-我编写了该代码)。

There are many ways you could do this. One quick and dirty way of doing it is to generate a string in your code and insert the values

eg

NSString *myXML = [NSString stringWithFormat:@"<myxml><value1>%@</value1><value2>%@</value2></myxml>", textfield1, textfield2];

Then open a file and write myXML to it.

Not very elegant, but it depends on what exactly you want.

Do you need the XML file to be in a specific format? If not the easiest way is to save it as a plist (a type of xml file) by putting your strings into an array and then saving the array as a plist using

NSArray *array = [NSArray arrayWithObjects:label1.text, label2.text, label3.text, etc, nil];
[array writeToFile:fileName atomically:YES];

The nice thing about the plist is that you can easily load the string again by calling:

NSArray *array = [NSArray arrayWithContentsOfFile:filename];

If you need your XML in a different format from what the plist produces, the easiest way is probably just to build the XML yourself using string concatenation, eg

NSString *xml = [NSSString stringWithFormat:@"<root><label>%@</label><label>%@</label><label>%@</label>etc</root>", label1.text, label2.text, label3.text, etc];

You can use GDATAXML to read and write XML to file. Refer this link to see how its done. http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM