应采取哪些步骤将我的 XML 转换为 Core Data 对象?

发布于 2024-08-21 17:47:49 字数 933 浏览 10 评论 0原文

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

我打算将这些对象存储在应用程序中以供将来使用(它们将是地图上的注释)。因此,每个重复字段都需要存储,其中一个是不够的。我知道如何在核心数据中对其进行建模,我相信,我将拥有一个电话号码实体和一个商店实体,并且将根据 将两者关联起来。我只是尝试使用一种简单的方法通过其他一些数据结构将它们从 XML 移动到 Core Data。

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>

如果可以采用键值对,请指出一种可以解释重复元素的方法。如果还有其他方法,我洗耳恭听。

谢谢

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.

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 sample:

<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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱她像谁 2024-08-28 17:47:49

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

设计好数据模型后,您可以使用 NSXMLParser 或 TouchXML 等第三方库来解析 XML。从你的元素开始。对于其中的每个项目,根据商店实体创建一个对象。对于元素中的每个电话,创建一个电话实体,依此类推。

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).

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.

疧_╮線 2024-08-28 17:47:49

我敢说,边解析边创建的方法可能不仅是最直接的,而且也是资源密集程度最低的方法。如果您可以使用面向流的解析器来处理 XML,并在完成解析时保存存储,那么您一次需要保存在内存中的内容就会少得多。

我还会认真考虑这些属性是否需要是成熟的实体,或者只是商店的属性。例如,除了将其显示在地图上之外,您还会对所有者的姓名执行任何操作吗? (是的,我看到你对那个特定事物的评论只是一个例子。)

不幸的是,核心数据并没有真正提供一种好方法来处理不是关系的多值属性。 (这里的电话号码确实看起来像是一个字符串数组。)尽管请参阅 这个问题这个 cocoa-dev 线程对此进行了一些讨论。

I'd venture that a create-as-you-parse approach is likely to be not only the most straightforward, but also the least less resource-intensive approach. If you can use a stream-oriented parser to handle the XML, and save the stores as you finishing parsing them, that's much less stuff you have to hold in memory at a time.

I'd also think seriously about whether the attributes need to be full-blown entities, or just properties of the store. For example, would you be doing anything with the owner's name beyond display it on the map? (And, yes, I saw your comment about that particular thing just being an example.)

Unfortunately, Core Data doesn't really provide a good way to do a multi-valued property that isn't a relationship. (Phone numbers here really seem like they could just be an array of strings.) Though see this SO question and this cocoa-dev thread for some discussion of that.

窗影残 2024-08-28 17:47:49

只是在基本层面上,不多......我真的无法弄清楚你在哪里指定这个,也许当你第一次创建模型时......但是数据存储的选择之一是.plist,它是用于所有密集目的的 XML...

例如...这个“老式”plist,直接从核心数据模型中提取...

(
Editorial,
News,
Retraction,
"FLAME!",
)

与此数据完全相同XML 设置,一旦通过

/usr/bin/plutil -convert xml1 /../input.plist -o /../output.xml运行

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>Editorial</string>
    <string>News</string>
    <string>Retraction</string>
    <string>FLAME!</string>
</array>
</plist>

Just on a basic level, not much.. I can't really figure out where you specify this, maybe when you first create the model.. but one of the choices for the data-store is as .plist, which is XML for all intensive purposes...

for example... this "old-school" plist, pulled directly from a core-data model..

(
Editorial,
News,
Retraction,
"FLAME!",
)

is the exact same data as this XML set, once run through

/usr/bin/plutil -convert xml1 /../input.plist -o /../output.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>Editorial</string>
    <string>News</string>
    <string>Retraction</string>
    <string>FLAME!</string>
</array>
</plist>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文