本文介绍了如何映射XML文本内容与RestKit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的XML:

 < rss& 
< channel>
< item>
< title> something< / title>
< description> ...< / description>
< category>烹饪< / category>
< category> housing< / category>
< / item>

< item>
...
< / item>
...
< / channel>
< / rss>

这是一个RSS源,包含一个通道中的项目数组,包含许多标签/属性,content,...),以及类别标签集合。



我可以使用RestKit解析频道/项目/标题/内容部分, / p>

  RKManagedObjectMapping * itemMapping = [RKManagedObjectMapping mappingForClass:[Item class] inManagedObjectStore:objectStore]; 
[itemMapping mapKeyPath:@titletoAttribute:@title];
[itemMapping mapKeyPath:@descriptiontoAttribute:@content];

RKManagedObjectMapping * channelMapping = [RKManagedObjectMapping mappingForClass:[Channel class] inManagedObjectStore:objectStore];
[channelMapping mapKeyPath:@itemtoRelationship:@itemswithMapping:itemMapping];

[manager.mappingProvider setMapping:channelMapping forKeyPath:@rss.channel];

但我失去了映射类别...我试过这样: / p>

  RKManagedObjectMapping * categoryMapping = [RKManagedObjectMapping mappingForClass:[Category class] inManagedObjectStore:objectStore]; 
[itemMapping mapKeyPath:@categorytoRelationship:@categorieswithMapping:CategoryMapping]

但是不知何故,标签内的文本内容没有映射​​到我的属性'name'里面的类类。确定我没有在这里的代码中使用这个名称,因为我不知道放在哪里。



如何解析纯文本XML标签与RestKit?是否有类似:

  [categoryMapping mapKeyPath:@.texttoAttribute:@name]; 



$ p

我不知道工作。


I have a XML like this:

<rss>
<channel>
 <item>
  <title>something</title>
  <description>...</description>
  <category>cooking</category>
  <category>housing</category>
 </item>

 <item>
  ...
 </item>
 ...
</channel>
</rss>

It's a RSS Feed, with an array of items inside a channel, containing many tag/properties (title, content,...), with a collection of category tags.

I can parse it with RestKit for the channel/item/title/content part with this:

RKManagedObjectMapping *itemMapping = [RKManagedObjectMapping mappingForClass:[Item class] inManagedObjectStore:objectStore];
[itemMapping mapKeyPath:@"title" toAttribute:@"title"];
[itemMapping mapKeyPath:@"description" toAttribute:@"content"];

RKManagedObjectMapping *channelMapping = [RKManagedObjectMapping mappingForClass:[Channel class] inManagedObjectStore:objectStore];
[channelMapping mapKeyPath:@"item" toRelationship:@"items" withMapping:itemMapping];

[manager.mappingProvider setMapping:channelMapping forKeyPath:@"rss.channel"];

But I'm at lost for mapping categories... I've tried something like that:

RKManagedObjectMapping *categoryMapping = [RKManagedObjectMapping mappingForClass:[Category class] inManagedObjectStore:objectStore];
[itemMapping mapKeyPath:@"category" toRelationship:@"categories" withMapping:CategoryMapping]

But somehow the text content inside the tag is not mapped to my propertie 'name' inside the Category class. For sure I didn't use this 'name' in the code here, because I don't know where to put it.

How can I parse text-only XML tag with RestKit? Is there something like:

[categoryMapping mapKeyPath:@".text" toAttribute:@"name"];

?

(doesn't work as is)

解决方案

you can try:

I'm not sure if it will work though.

这篇关于如何映射XML文本内容与RestKit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:13
查看更多