[英]How to save data into an XML file
My application contains 8 TextFields. 我的应用程序包含8个TextField。 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).
当我单击提交按钮时,我想将TextField中的所有值保存到XML文件中(并在需要时生成XML文件)。 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. 然后打开一个文件并将myXML写入其中。
Not very elegant, but it depends on what exactly you want. 不是很优雅,但这取决于您到底想要什么。
Do you need the XML file to be in a specific format? 您是否需要XML文件采用特定格式? 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
如果不是,最简单的方法是通过将字符串放入数组中,然后将其另存为plist,将其另存为plist(一种xml文件)。
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: plist的好处是,您可以通过调用以下命令轻松地再次加载字符串:
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 如果您需要的XML格式与plist生成的格式不同,那么最简单的方法可能就是使用字符串连接来自己构建XML,例如
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. 您可以使用GDATAXML读取XML并将其写入文件。 Refer this link to see how its done.
请参阅此链接以了解其效果。 http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml
http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.