Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios RestKit使用数组解析atom XML_Ios_Objective C_Xml_Restkit_Atom Feed - Fatal编程技术网

Ios RestKit使用数组解析atom XML

Ios RestKit使用数组解析atom XML,ios,objective-c,xml,restkit,atom-feed,Ios,Objective C,Xml,Restkit,Atom Feed,我尝试使用RestKit(0.23.1)解析一些Atom XML,如下所示: <feed xmlns="http://www.w3.org/2005/Atom" xmlns:ex="http://my.library.com"> <entry> <title>Title 1</title> <link href="http://localhost:2001/1" /> &l

我尝试使用RestKit(0.23.1)解析一些Atom XML,如下所示:

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:ex="http://my.library.com">    
    <entry>
        <title>Title 1</title>
        <link href="http://localhost:2001/1" />
        <id>urn:uuid:a6e04980-e273-11e3-8b68-0800200c9a66</id>
        <updated>2013-12-21T18:30:02Z</updated>
        <summary>Summary 1</summary>
        <ex:thing>
            <ex:name>Thing 1</ex:name>
        </ex:thing>
        <ex:thing>
            <ex:name>Thing 2</ex:name>
        </ex:thing>
    </entry>
</feed>
NSMutableArray包含事物的集合

@interface Thing : NSObject

@property NSString *name;

@end
最后,将这一切粘合在一起的RestKit代码如下:

- (void) parseAtomThings {
    [RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/atom+xml"];

    RKObjectMapping *entryMapping = [RKObjectMapping mappingForClass:[Entry class]];
    [entryMapping addAttributeMappingsFromDictionary:@{
            @"title.text" : @"title",
            @"summary.text" : @"summary"
    }];

    RKObjectMapping *thingMapping = [RKObjectMapping mappingForClass:[Thing class]];
    [thingMapping addAttributeMappingsFromDictionary:@{
            @"name.text" : @"name"
    }];

    // TODO - how do I get this to map to ex:thing?
    [entryMapping addRelationshipMappingWithSourceKeyPath:@"thing" mapping:thingMapping];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entryMapping method:RKRequestMethodGET pathPattern:nil
                                                          keyPath:@"feed.entry" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:2001/library.xml"]];

    RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
    [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
         for(Entry *entry in [result array]) {
            NSLog(@"%@", entry.description);
         }
     } failure:^(RKObjectRequestOperation *operation, NSError *error) {
         NSLog(@"Failed with error: %@", [error localizedDescription]);
     }];
    [operation start];
}
执行时,将填充条目对象的标题和摘要属性。但是,NSMutableArray没有填充,并且为零

我相信我要寻找的魔法就在这条线上:

[entryMapping addRelationshipMappingWithSourceKeyPath:@"thing" mapping:thingMapping];
但我不知道如何正确指定Entry类上thing属性的映射列表

日志记录在这里:

2014-05-23 23:06:05.457 TestApp[61282:3303] D restkit.object_mapping:RKMappingOperation.m:859 Starting mapping operation...
2014-05-23 23:06:05.457 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:860 Performing mapping operation: <RKMappingOperation 0xbe3b4b0> for 'Entry' object. Mapping values from object {
    "ex:thing" =     (
                {
            "ex:name" =             {
                text = "Thing 1";
            };
            text = "";
        },
                {
            "ex:name" =             {
                text = "Thing 2";
            };
            text = "";
        }
    );
    id =     {
        text = "urn:uuid:a6e04980-e273-11e3-8b68-0800200c9a66";
    };
    link =     {
        href = "http://localhost:2001/1";
        text = "";
    };
    summary =     {
        text = "Summary 1";
    };
    text = "";
    title =     {
        text = "Title 1";
    };
    updated =     {
        text = "2013-12-21T18:30:02Z";
    };
} to object <Entry: 0xbe39e00> with object mapping (null)
2014-05-23 23:06:05.458 TestApp[61282:3303] D restkit.object_mapping:RKMappingOperation.m:728 Did not find mappable relationship value keyPath 'thing'
2014-05-23 23:06:05.459 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'summary.text'. Transforming from class '__NSCFString' to 'NSString'
2014-05-23 23:06:05.459 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'summary.text' to 'summary'
2014-05-23 23:06:05.459 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'summary.text' to 'summary'. Value: Summary 1
2014-05-23 23:06:05.459 TestApp[61282:e03] D restkit.object_mapping:RKPropertyInspector.m:131 Cached property inspection for Class 'Entry': {
    summary =     {
        isPrimitive = 0;
        keyValueCodingClass = NSString;
        name = summary;
    };
    things =     {
        isPrimitive = 0;
        keyValueCodingClass = NSMutableArray;
        name = things;
    };
    title =     {
        isPrimitive = 0;
        keyValueCodingClass = NSString;
        name = title;
    };
}
2014-05-23 23:06:05.460 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'title.text'. Transforming from class '__NSCFString' to 'NSString'
2014-05-23 23:06:05.460 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'title.text' to 'title'
2014-05-23 23:06:05.461 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'title.text' to 'title'. Value: Title 1
2014-05-23 23:06:05.461 TestApp[61282:3303] D restkit.object_mapping:RKMappingOperation.m:928 Finished mapping operation successfully...
2014-05-23 23:06:05.461 TestApp[61282:3303] D restkit.object_mapping:RKMapperOperation.m:403 Finished performing object mapping. Results: {
    "feed.entry" = "<Entry: 0xbe39e00>";
}

我猜我看不见树木,错过了一些非常明显的东西。非常感谢您的帮助。

您的关键路径中缺少了
示例:

为此,您需要创建一个关系映射并添加它(使用
relationshipMappingFromKeyPath:toKeyPath:withMapping:
),而不是使用方便的方法
addRelationshipMappingWithSourceKeyPath:mapping:
(因为源路径和目标路径不同)


同样的问题也延伸到了名称,您需要将
@“name.text”
更改为
@“ex:name.text”

谢谢您的指针,它现在工作得很好。正如您所建议的,我将addRelationshipMappingWithSourceKeyPath替换为:
RKRelationshipMapping*relationshipMapping=[RKRelationshipMapping relationshipMappingFromKeyPath:@“ex:thing”tokePath:@“things”withMapping:thingMapping]
[entryMapping addPropertyMapping:relationshipMapping]
2014-05-23 23:06:05.457 TestApp[61282:3303] D restkit.object_mapping:RKMappingOperation.m:859 Starting mapping operation...
2014-05-23 23:06:05.457 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:860 Performing mapping operation: <RKMappingOperation 0xbe3b4b0> for 'Entry' object. Mapping values from object {
    "ex:thing" =     (
                {
            "ex:name" =             {
                text = "Thing 1";
            };
            text = "";
        },
                {
            "ex:name" =             {
                text = "Thing 2";
            };
            text = "";
        }
    );
    id =     {
        text = "urn:uuid:a6e04980-e273-11e3-8b68-0800200c9a66";
    };
    link =     {
        href = "http://localhost:2001/1";
        text = "";
    };
    summary =     {
        text = "Summary 1";
    };
    text = "";
    title =     {
        text = "Title 1";
    };
    updated =     {
        text = "2013-12-21T18:30:02Z";
    };
} to object <Entry: 0xbe39e00> with object mapping (null)
2014-05-23 23:06:05.458 TestApp[61282:3303] D restkit.object_mapping:RKMappingOperation.m:728 Did not find mappable relationship value keyPath 'thing'
2014-05-23 23:06:05.459 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'summary.text'. Transforming from class '__NSCFString' to 'NSString'
2014-05-23 23:06:05.459 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'summary.text' to 'summary'
2014-05-23 23:06:05.459 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'summary.text' to 'summary'. Value: Summary 1
2014-05-23 23:06:05.459 TestApp[61282:e03] D restkit.object_mapping:RKPropertyInspector.m:131 Cached property inspection for Class 'Entry': {
    summary =     {
        isPrimitive = 0;
        keyValueCodingClass = NSString;
        name = summary;
    };
    things =     {
        isPrimitive = 0;
        keyValueCodingClass = NSMutableArray;
        name = things;
    };
    title =     {
        isPrimitive = 0;
        keyValueCodingClass = NSString;
        name = title;
    };
}
2014-05-23 23:06:05.460 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'title.text'. Transforming from class '__NSCFString' to 'NSString'
2014-05-23 23:06:05.460 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'title.text' to 'title'
2014-05-23 23:06:05.461 TestApp[61282:3303] T restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'title.text' to 'title'. Value: Title 1
2014-05-23 23:06:05.461 TestApp[61282:3303] D restkit.object_mapping:RKMappingOperation.m:928 Finished mapping operation successfully...
2014-05-23 23:06:05.461 TestApp[61282:3303] D restkit.object_mapping:RKMapperOperation.m:403 Finished performing object mapping. Results: {
    "feed.entry" = "<Entry: 0xbe39e00>";
}
014-05-23 23:06:05.458 TestApp[61282:3303] D restkit.object_mapping:RKMappingOperation.m:728 Did not find mappable relationship value keyPath 'thing'