4
我在RESTKit的潜力上销售 - 听起来很棒。使用RESTKit解析深层XML结构
不幸的是,我所能找到的所有例子都是围绕解析JSON和解析相对“平坦”的JSON。不幸的是,这个世界的现实是有很多的XML,而且很多都很丑陋。
特别是,我似乎无法找到任何解析深层嵌套XML结构的好例子。
我认为我的失败在于理解关键路径,我希望这个问题的答案将成为其他面临这个问题的人的典范。
让我们来看一个人为的例子吧。考虑下面的XML:
<?xml version="1.0"?>
<old_lady fate="perhaps she'll die!">
<bird reason = "to catch the spider">
<spider reason="to catch the fly">
<fly reason="why oh why?" action="swallowed" name="Al"/>
<fly reason="why oh why?" action="swallowed" name="Bob"/>
<fly reason="why oh why?" action="swallowed" name="Cory"/>
<fly reason="why oh why?" action="swallowed" name="Dan"/>
<fly reason="why oh why?" action="swallowed" name="Edgar"/>
</spider>
</bird>
</old_lady>
比方说,我想摆脱这种飞对象的列表:
@interface Fly : NSObject
@property (retain) NSString *reason;
@property (retain) NSString *action;
@property (retain) NSString *name;
@end
所以我想我会做这样的事情:
[RKObjectManager objectManagerWithBaseURL:[NSURL
URLWithString:@"http://some.server.com"]];
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Fly class]];
[mapping mapKeyPath:@"reason" toAttribute:@"reason"];
[mapping mapKeyPath:@"action" toAttribute:@"action"];
[mapping mapKeyPath:@"name" toAttribute:@"name"];
[RKObjectManager.sharedManager.mappingProvider setMapping:mapping
forKeyPath:@"//old_lady/bird/spider/fly"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/path/to/rhyme.xml"
delegate:self];
然而,这让我:
Encountered errors during mapping: Could not find an object mapping for keyPath: ''
我确定问题是我的'// old_lady/bird/spider/fly'键路径,但我找不到任何类似的例子来模拟解决方案。 HALP?
谢谢。你拯救我的一天:) – slobodans