应该采取什么步骤将我的XML转换为Core Data对象? [英] What steps should be taken to convert my XML into Core Data objects?

查看:92
本文介绍了应该采取什么步骤将我的XML转换为Core Data对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含商店列表的XML文件,简化版本如下。我想要帮助的是一些关于将这些数据移动到Core Data中存储的对象的最简单方法的高层想法。我看到关于键值对的建议,但正如你可以看到在我的例子中,我有相同的名称/键的子元素,每个存储元素可以有任意数量的这些。

I have an XML file which contains lists of stores, a simplified version is below. What I would like help with is some high-level ideas on the simplest ways to move this data into objects for storage in Core Data. I see suggestions around key-value pairs but as you can see in my example below, I have child elements with the same name/key and there can be an arbitrary number of these for each store element.

我打算将这些对象存储在应用程序中以供将来使用(他们将在地图上注释)。因此,每个重复字段需要存储,其中一个不足够。我知道如何在Core Data中建模我相信,我将有一个电话号码实体和一个商店实体,并且将基于< store-id> 。我只是试图使用一个简单的方法通过一些其他数据结构将它们从XML移动到Core Data。

I intend to store these objects within the app for future use (they will be annotations on a map). So, each duplicate field needs to be stored, one of them will not suffice. I know how to model it in Core Data I believe, I'll have a phone-number entity and a store entity and will just relate the two based on <store-id>. I'm just trying to use a simple method to move them from XML to Core Data via some other data structure.

XML示例:

<stores>
  <store>
   <store-id>1</store-id>
   <city>Dublin</city>
   <phone>011234567</phone>
   <phone>011234566</phone>
   <owner>Joe Bloggs</owner>
  </store>
  <store>
   <store-id>2</store-id>
   <city>Cork</city>
   <phone>019876543</phone>
   <phone>019876542</phone>
   <owner>Joe Bloggs</owner>
  </store>
<stores>

如果键值对是可行的,请指向一个方法,为重复元素。

If key-value pairs are the way to go, please point me to a method where I can account for the duplicate elements. If there's another way, I'm all ears.

感谢

推荐答案

p>更难的部分将是设计一个有意义的数据模型。你可能需要一个实体的商店,一个电话,和可能一个所有者。商店应该是一对多的电话号码,并与所有者一对一(只查看您的数据)。

The harder part is going to be designing a data model that makes sense. You'll probably need an entity for store, one for phone, and probably one for owner. Store should be one-to-many with phone number, and one-to-one with owner (just looking at your data).

一旦你有了数据模型out,那么您可以使用NSXMLParser或第三方库(如TouchXML)来解析XML。从您的元素开始。对于其中的每个项目,根据商店实体创建一个对象。对于元素中的每个电话,创建电话实体,等等。

Once you have the data model laid out, then you can use either NSXMLParser or a third party library like TouchXML to parse the XML. Start with your element. For each item in there, create an object based on the store entity. For each phone in the element, create a phone entity, and so on.

这篇关于应该采取什么步骤将我的XML转换为Core Data对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆