带有本地 XML 的 RestKit

Posted

技术标签:

【中文标题】带有本地 XML 的 RestKit【英文标题】:RestKit with local XML 【发布时间】:2012-09-17 07:17:51 【问题描述】:

我使用 RestKit 从资源加载 XML 数据。这很完美,但现在我想改变我的整个程序来读取本地 XML 文件。我发现可以读取本地 xml 文件并通过 Google RestKit Group 中的 RestKit 对其进行解析:

NSString *string = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"testing" ofType:@"xml"] encoding:NSUTF8StringEncoding error:nil]; 
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeXML]; 
id result = [parser objectFromString:string error:nil]; 
NSLog(@"%@",[[[[result objectForKey:@"sample"] objectForKey:@"nested"] objectForKey:@"tags" ] description]); 

但现在我想用我的映射加载这个对象。我从服务器加载的旧代码:

RKObjectManager *objectManager = [RKObjectManager sharedManager];
RKURL *URL = [RKURL URLWithBaseURL:[objectManager baseURL] resourcePath:[xmlFile path] queryParameters:nil];

[objectManager.mappingProvider setMapping:myMapping forKeyPath:myKeyPath];

[objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@", [URL resourcePath]] usingBlock:^(RKObjectLoader *loader) 
    loader.delegate = self;
];

之后,我在 RestKit 的 didLoadObjects 委托中获得了我的映射对象。我怎样才能为我的本地 xml 对象做同样的事情?

【问题讨论】:

【参考方案1】:

我找到了以下问题的解决方案。像魅力一样工作。

RKObjectMappingProvider* mappingProvider = [RKObjectManager sharedManager].mappingProvider;
RKObjectMapper* mapper = [RKObjectMapper mapperWithObject:parsedData mappingProvider:mappingProvider];
RKObjectMappingResult* result = [mapper performMapping];
if (result) 

    NSArray *resultArray = result.asCollection;

    MyMappingClass *object = [resultArray lastObject];
    NSLog(@"My Object: %@", object);

【讨论】:

【参考方案2】:

对于 Restkit 0.20,我使用这种方法:

库.xml

<?xml version="1.0" encoding="UTF-8"?>
<library>
    <bookname>azeaze</bookname>
    <bookname>qdd</bookname>
    <bookname>vvef</bookname>
    <bookname>ytrtefc</bookname>
</library>

书.h

#import <Foundation/Foundation.h>

@interface Book : NSObject

@property (nonatomic,strong) NSString * label;

@end

书.m

#import "Book.h"

@implementation Book

@end

我用这个 XML 文件创建了一个 NSDictionary,为此我使用XMLReader

#import "XMLReader.h"

[...]

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Book class]];
[mapping addAttributeMappingsFromDictionary:@@"bookname": @"label",];

NSString *pathToDataFile = [[NSBundle mainBundle] pathForResource:@"library"
                                                           ofType:@"xml"];

NSError *error;

NSString* contents = [NSString stringWithContentsOfFile:pathToDataFile
                                               encoding:NSUTF8StringEncoding
                                                  error:&error];

NSData* xmlData = [contents dataUsingEncoding:NSUTF8StringEncoding];

NSError *parseError = nil;

NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:xmlData
                                                        error:&parseError];

NSDictionary *mappingsDictionary = @ @"library": mapping ;

RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:xmlDictionary
                                                           mappingsDictionary:mappingsDictionary];

NSError *mappingError = nil;

BOOL isMapped = [mapper execute:&mappingError];

if (isMapped && !mappingError) 
    NSLog(@"SUCCESS : %@",[mapper mappingResult]);
 else 
    NSLog(@"FAIL : %@",mappingError);

【讨论】:

以上是关于带有本地 XML 的 RestKit的主要内容,如果未能解决你的问题,请参考以下文章

Maven更新后本地仓库jar后缀带有 lastUpdated

将本地 XML 或 Shapefile 加载到 OSMNX 以创建图形

本地化资源可以在运行时下载吗?

谁用过Xstream 用其生成带有cdata标签的xml解析带有cdata标签的xml

本地读取服务器Xml文件及本地读本地的xml

WebService初入