Iphone 我的应用程序中有xml文件,我想将数据写入其中
我有一个xml文件:Iphone 我的应用程序中有xml文件,我想将数据写入其中,iphone,objective-c,xml-parsing,Iphone,Objective C,Xml Parsing,我有一个xml文件: <?xml version="1.0" encoding="UTF-8"?> <Favorites> <favorite id="1"> <title>My first favorite</title> <latitude>31.369834</latitude> <longitude>34.798207</longi
<?xml version="1.0" encoding="UTF-8"?>
<Favorites>
<favorite id="1">
<title>My first favorite</title>
<latitude>31.369834</latitude>
<longitude>34.798207</longitude>
</favorite>
</Favorites>
我最喜欢的
31.369834
34.798207
我想在里面写更多的“最爱”。
在我的项目中,我拥有作为字符串所需的所有数据。
但我不知道如何真正做到这一点——尽管我已经尝试了很多
你能帮我做吗?
谢谢。您可以使用plist并创建以下函数:
First - creates an array or dictionary from the plist
Second - adds new values to your array/dictionary
Third - saves the dictionary to the same file, overwriting and updating the old version.
<> >而不是用刚刚更新的版本插入值,我同意响应者,您应该考虑使用.PLIST文件。创建“收藏”对象的NSDictionary对象,并保存它们的数组 但是,对于您眼前的问题,您可以像这样保存表示XML的NSString(但同样,保存基于字符串的XML意味着您创建了许多解析和交互方法):
//构建路径
//
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
是的);
NSString*filePath=[path objectAtIndex:0];
filePath=[filePath stringByAppendingPathComponent:@“收藏夹”];
filePath=[filePath stringByAppendingPathExtension:@“xml”];
//你应该有一个“最喜欢”的对象,知道如何描述自己。。。
//
NSString*badWayToDoThis=@“将数据保存到plist”;
[badWayToDoThis writeToFile:文件路径
原子论:是的
编码:NSUTF8StringEncoding
错误:NULL];
希望这至少能帮助您朝着正确的方向前进。这可能会有所帮助:如果您对使用XML没有任何外部要求,Alex Nichol的建议是一个很好的建议。您也可以使用JSON,因为iOS包含用于创建和读取JSON的API(从iOS5开始)。您好,首先谢谢您,我理解您在这里告诉我的内容-但无法理解-
code
//您应该有一个“最喜欢的”对象,知道如何描述自己…您说您的项目中所有数据都是字符串。为此,数据应该是存储在NSObject中的每个“收藏夹”项的字符串。在该对象内部应该有一个方法,该方法以您需要的形式(NSDictionary、NSString等)返回对象的数据-在上述示例中,可以重写NSOBject的“description”方法以返回字符串。
// Build The Path
//
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString * filePath = [paths objectAtIndex:0];
filePath = [filePath stringByAppendingPathComponent:@"favorites"];
filePath = [filePath stringByAppendingPathExtension:@"xml"];
// You Should Have A "Favorite" Objects That Know How To Desribe Themselves...
//
NSString * badWayToDoThis = @"<ThinkAboutUsingDictionaries>Saving The Data to a plist</seriously>";
[badWayToDoThis writeToFile:filePath
atomically:YES
encoding:NSUTF8StringEncoding
error:NULL];