How to map XML textContent with RestKit?

2019-05-11 10:25发布

问题:

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)

回答1:

you can try:

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

I'm not sure if it will work though.