使用RKXMLReaderSerialization时找不到可映射的值

时间:2013-05-31 05:14:23

标签: ios xml core-data restkit-0.20

我有一个使用Restkit 0.20.1的iOS应用程序以及RKXMLReaderSerialization 0.20.0来从服务器提取xml格式的数据。如果服务器向我发送JSON数据,但代码工作得很好,但现在我试图以XML格式提取数据,我已经走到了尽头。我仍然从服务器接收数据,但我收到的错误是:

restkit.object_mapping:RKMapperOperation.m:98 Adding mapping error: No mappable values found for any of the attributes or relationship mappings

我假设这意味着对象映射器不会将任何数据识别为与我的核心数据属性匹配,因此没有任何要映射的内容。我通过cocoapods安装了RKXMLReaderSerialization并根据文档注册了该类。但显然我错过了一些东西。有人能指出来吗?

这是我注册序列化类的地方

// Set the default store shared instance
    [RKManagedObjectStore setDefaultStore:managedObjectStore];

    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://www.myserver.com"]];
    [RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];

这是映射和响应描述符部分:

RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Gist" inManagedObjectStore:managedObjectStore];
    [entityMapping addAttributeMappingsFromDictionary:@{
     @"articleId":      @"gistID",
     @"title":          @"title",
     @"hashtags":       @"hashtags",
     @"imageUrl":       @"imageUrl",
     @"summary":        @"summary"}];
    entityMapping.identificationAttributes = @[ @"gistID" ];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping pathPattern:@"/rest/article/getTicker" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

以下是适用的日志:

restkit.object_mapping:RKMapperOperation.m:98 Adding mapping error: No mappable values    found for any of the attributes or relationship mappings
restkit.network:RKObjectRequestOperation.m:241 GET 'http://www.myserver.com/rest/article/getTicker?ip=255.255.255.0' (200 OK / 0 objects) [request=0.2420s mapping=0.0151s total=0.2737s]:
    response.headers={
        Connection = "Keep-Alive";
        "Content-Type" = "application/xml";
        Date = "Fri, 31 May 2013 04:30:50 GMT";
        "Keep-Alive" = "timeout=5, max=100";
        "Transfer-Encoding" = Identity;
    }
    response.body=<?xml version="1.0" encoding="UTF-8" standalone="yes"?><tickers><ticker><articleId>7587</articleId><authorId>10</authorId><authorName>AFP  </authorName><city>Kabul</city><copyrightLine>Copyright 2012 AFP</copyrightLine><countryCode>US</countryCode><countryName>AF</countryName><hashTags>#Afghanistan #unrest #art #offbeat </hashTags><imageUrl>http://www.mywebsite.com/services/images/AFP/photo_1369471279196-2-2.jpg</imageUrl><latitude>34.52813</latitude><longitude>69.17233</longitude>

正如您所看到的,响应主体以XML格式显示我正在寻找的核心数据属性(articleId,title,hashtags,...)但它不能通过xmlreader运行主体....我对于iOS和Objective-C来说,这是一个很新的东西,所以任何帮助都是值得赞赏的,而且片段的价值也很重要! 感谢

更新... 以下是xml如何来自服务器

<?xml version="1.0" encoding="UTF-8"?>
<tickers>
    <ticker>
        <articleId>7587</articleId>
        <authorId>10</authorId>
        <authorName>AFP </authorName>
        <city>Kabul</city>
        <copyrightLine>Copyright 2012 AFP</copyrightLine>
        <countryCode>US</countryCode>
        <countryName>AF</countryName>
        <hashTags>#Afghanistan #unrest #art #offbeat </hashTags>
        <imageUrl>http://www.mywebsite.com/services/images/AFP/photo_1369471279196-2-2.jpg</imageUrl>
        <latitude>34.52813</latitude>
        <longitude>69.17233</longitude>
        <title>Day after Kabul attacks, 10,000 peace balloons</title>
        <totComments>0</totComments>
        <totDislikes>0</totDislikes>
        <totInappropriate>0</totInappropriate>
        <totLikes>0</totLikes>
        <totViews>0</totViews>
    </ticker>

更新#2 .... 不同之处在于,当我收到XML与从服务器接收JSON时,响应的映射方式。

序列化

tickers =     {
    ticker =         (
                    {
            articleId =                 {
                text = 7587;
            };
            authorId =                 {
                text = 10;
            };
            authorName =                 {
                text = AFP;
            };
            city =                 {
                text = Kabul;
            };
            copyrightLine =                 {
                text = "Copyright 2012 AFP";
            };
           .....Goes on like this for each attribute

JSON直接来自服务器

    {
    articleId = 7587;
    authorId = 10;
    authorName = "AFP  ";
    city = Kabul;
    copyrightLine = "Copyright 2012 AFP";
    countryCode = US;
    countryName = AF;
    hashTags = "#Afghanistan #unrest #art #offbeat ";
    imageUrl = "http://www.mywebsite.com/services/images/AFP/photo_1369471279196-2-2.jpg";
    latitude = "34.52813";
    longitude = "69.17233";
    title = "Day after Kabul attacks, 10,000 peace balloons";
    totComments = 0;
    totDislikes = 0;
    totInappropriate = 0;
    totLikes = 0;
    totViews = 0;
},

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我认为序列化可能很好。我认为问题在于映射没有足够的信息来导航XML(我假设它具有比相应的JSON更多的标签)。

尝试告诉响应描述符XML中数据位置的关键路径:

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping pathPattern:@"/rest/article/getTicker" keyPath:@"tickers.ticker" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

更新:您的映射需要是:

[entityMapping addAttributeMappingsFromDictionary:@{
 @"articleId.text":      @"gistID",
 @"title.text":          @"title",
 @"hashtags.text":       @"hashtags",
 @"imageUrl.text":       @"imageUrl",
 @"summary.text":        @"summary"}];