0

我有一个 iOS 应用程序使用 Restkit 0.20.1 和 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;
},

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

4

1 回答 1

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"}];
于 2013-05-31T06:24:05.823 回答