简体   繁体   English

将XML写入项目(iOS,Xcode 4)

[英]Write XML to Project (iOS, Xcode 4)

I think there just isn't a solution to my problem, but I was hoping someone might have a creative work-a-round from past experience to share. 我认为这还不能解决我的问题,但是我希望有人可以从过去的经验中分享出自己的创意。 I have created a 3D level editor for my latest iPhone/iPad game, and I am at the final stage where I need to write the map out into an XML file format I've devised. 我已经为我的最新iPhone / iPad游戏创建了3D关卡编辑器,并且在最后阶段,需要将地图写成我设计的XML文件格式。

I'm using KISSXML as my XML library for parsing, and I'm getting ready to extend my use of it to writing as well. 我将KISSXML用作解析的XML库,并且准备将其用途扩展到编写。 My question is this: is it possible to somehow write to a location that I can view and then store in the project? 我的问题是:是否可以以某种方式写入我可以查看并存储在项目中的位置? For the initial 20-30 map files I create, I want to directly import them into the project as resources that could be edited later as text files too... I don't want them stored as app resources yet at this stage (but I've heard you cannot write from an app to the resources structure for a project?). 对于我创建的最初的20-30个地图文件,我想将它们作为资源直接导入到项目中,以后也可以将其编辑为文本文件...在此阶段,我不希望它们作为应用程序资源存储(但我听说您无法从应用程序写入项目的资源结构?)。 Is there some creative way to even just send myself the XML or write it in a location I can retrieve it easily? 是否有某种创造性的方法,甚至可以仅向自己发送XML或将其写入可以方便地检索到的位置? When I extend the editor to be a usable tool by players, writing to the app directory will be great - but for now, that is not what I'm looking for. 当我将编辑器扩展为玩家可以使用的工具时,写入app目录将是很棒的-但就目前而言,这不是我想要的。

Any suggestions or tips would be fantastic! 任何建议或提示将是太棒了!

Here's a snippet I'm using to write out at the moment. 这是我目前正在写的摘录。 I guess an even simpler question is: where does this go? 我想一个更简单的问题是:这去哪儿了? Is it somewhere I can access outside of my app? 我可以在我的应用程序之外访问吗? Is there some way to write it so that it is more accessible to me? 有什么办法可以编写它,以便我更容易使用?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:@"%@/xmlfile.xml", documentsDirectory];

DDXMLDocument* document = [[[DDXMLDocument alloc] initWithXMLString:@"<addresses/>" options:0 error:nil] autorelease];
DDXMLElement* root = [document rootElement];
[root addChild:[DDXMLNode elementWithName:@"address" stringValue:@"Some Address"]];

NSData *xmlData = [document XMLDataWithOptions:DDXMLNodePrettyPrint];
if (![xmlData writeToFile:fileName atomically:YES]) {
    NSLog(@"Could not write document out...");
}

UPDATE UPDATE

Using the following line: 使用以下行:

NSLog(@"Documents: %@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]);

I can get access via finder in OSX to my documents directory and view/edit the XML file I've written. 我可以通过OSX中的finder访问我的文档目录,并查看/编辑我编写的XML文件。 This of course only works when I generate the file using the simulator, not the device. 当然,这仅在我使用模拟器而不是设备生成文件时才有效。 This is looking more and more like a problem without a solution... I suppose I could email the XML file as an attachment to myself as a cheap hack? 这看起来越来越像一个没有解决方案的问题...我想我可以将XML文件作为附件发送给我,作为廉价的黑客? Not my preferred solution, but may have to do for now. 这不是我的首选解决方案,但可能现在需要这样做。

From my understanding, you want to inspect the files your app creates. 据我了解,您想检查应用程序创建的文件。 Several possible solutions come to mind. 我想到了几种可能的解决方案。 You could 你可以

  1. enable Itunes Files Sharing by setting the UIFileSharingEnabled property in your application's info.plist. 通过在应用程序的info.plist中设置UIFileSharingEnabled属性来启用Itunes文件共享。
  2. implement Dropbox 实施Dropbox
  3. make your app post the file to a webserver you have in your local network. 让您的应用将文件发布到本地网络中的网络服务器上。

Option 1 is probably the easiest to setup, but you have to make some clicks to get to your data. 选项1可能是最容易设置的,但是您必须单击一下才能获取数据。 If you have a webserver running anyway I would probably go for option 3. 如果您有正在运行的Web服务器,我可能会选择选项3。

Without hearing from anyone, I'm assuming this is fairly impossible to do. 如果没有任何人的来信,我假设这是相当不可能的。 I'm switching gears and now both A) saving the map to the apps directory for future loading, and B) emailing a copy of the XML to myself for extraction and then manual integration with the project. 我正在换档,现在都A)将地图保存到apps目录中以供将来加载,以及B)通过电子邮件将XML副本发送给我自己以进行提取,然后与项目手动集成。 Not the ideal solution I hoped for, but it does work. 不是我希望的理想解决方案,但是它确实有效。

Hope this helps anyone else curious down the road. 希望这可以帮助其他好奇的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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