菜鸟教程小白 发表于 2022-12-12 22:24:19

ios - RestKit 用数组解析 atom XML


                                            <p><p>我正在尝试使用 RestKit (0.23.1) 来解析一些 Atom XML,如下所示:</p>

<pre><code>&lt;feed xmlns=&#34;http://www.w3.org/2005/Atom&#34; xmlns:ex=&#34;http://my.library.com&#34;&gt;   
    &lt;entry&gt;
      &lt;title&gt;Title 1&lt;/title&gt;
      &lt;link href=&#34;http://localhost:2001/1&#34; /&gt;
      &lt;id&gt;urn:uuid:a6e04980-e273-11e3-8b68-0800200c9a66&lt;/id&gt;
      &lt;updated&gt;2013-12-21T18:30:02Z&lt;/updated&gt;
      &lt;summary&gt;Summary 1&lt;/summary&gt;
      &lt;ex:thing&gt;
            &lt;ex:name&gt;Thing 1&lt;/ex:name&gt;
      &lt;/ex:thing&gt;
      &lt;ex:thing&gt;
            &lt;ex:name&gt;Thing 2&lt;/ex:name&gt;
      &lt;/ex:thing&gt;
    &lt;/entry&gt;
&lt;/feed&gt;
</code></pre>

<p>我正在使用对象映射器将馈送映射到两个对象。第一个对象表示提要中的一个条目,其中包含一组事物。</p>

<pre><code>@interface Entry : NSObject

@property NSString *title;
@property NSString *summary;
@property NSMutableArray *things;

@end
</code></pre>

<p>NSMutableArray 是包含事物的集合。 </p>

<pre><code>@interface Thing : NSObject

@property NSString *name;

@end
</code></pre>

<p>最后,将这一切粘合在一起的 RestKit 代码在这里:</p>

<pre><code>- (void) parseAtomThings {
    forMIMEType:@&#34;application/atom+xml&#34;];

    RKObjectMapping *entryMapping = ];
    [entryMapping addAttributeMappingsFromDictionary:@{
            @&#34;title.text&#34; : @&#34;title&#34;,
            @&#34;summary.text&#34; : @&#34;summary&#34;
    }];

    RKObjectMapping *thingMapping = ];
    [thingMapping addAttributeMappingsFromDictionary:@{
            @&#34;name.text&#34; : @&#34;name&#34;
    }];

    // TODO - how do I get this to map to ex:thing?
    ;

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entryMapping method:RKRequestMethodGET pathPattern:nil
                                                          keyPath:@&#34;feed.entry&#34; statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    NSURLRequest *request = ];

    RKObjectRequestOperation *operation = [ initWithRequest:request responseDescriptors:@];
    [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
         for(Entry *entry in ) {
            NSLog(@&#34;%@&#34;, entry.description);
         }
   } failure:^(RKObjectRequestOperation *operation, NSError *error) {
         NSLog(@&#34;Failed with error: %@&#34;, );
   }];
    ;
}
</code></pre>

<p>在执行时,Entry 对象的标题和摘要属性会被填充。然而,事物的 NSMutableArray 没有被填充并且是 nil。</p>

<p>我确定我正在寻找的魔法就在这条线上:</p>

<pre><code>;
</code></pre>

<p>但我不知道如何正确地将映射列表指定到 Entry 类的 thing 属性。</p>

<p>日志在这里:</p>

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

<p>日志清楚地表明:</p>

<pre><code>014-05-23 23:06:05.458 TestApp D restkit.object_mapping:RKMappingOperation.m:728 Did not find mappable relationship value keyPath &#39;thing&#39;
</code></pre>

<p>我猜我只见树木不见森林,错过了一些非常明显的东西。非常感谢任何帮助。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您的关键路径中缺少 <code>ex:</code>。</p>

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

<p>同样的问题延伸到您需要将 <code>@"name.text"</code> 更改为 <code>@"ex:name.text"</code> 的名称。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - RestKit 用数组解析 atom XML,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23839028/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23839028/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - RestKit 用数组解析 atom XML