此框架旨在简化 SOAP 请求的构建以及响应解析/映射到领域对象。
解析基于 Google 开发的 GDataXMLNode 类。一个方便的 XML 解析类,会生成良好且有结构的输出。实际上,您会看到由于结果已经被解析,可以直接使用结果而无需将其映射到对象。
映射功能基于 dchohfi 编写的 KeyValueObjectMapping。基本上是一种用于处理 SoapKit 结果类的改编。
以下示例主要来自示例项目,这应该有助于理解
对于此示例,我们使用一个免费可用的 webservice(WSDL),构建 SKRequest
并发送。
- (void)loadItemsofType:(NSString *)type withinLast:(NSUInteger)days
onCompletion:(void (^)(NSArray *lectures, NSError *error))completion {
SKRequest *request = [[SKRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.foxcentral.net/foxcentral.asmx"] operation:@"GetNewsItems" andNamespaceURL:[NSURL URLWithString:@"http://www.west-wind.com/foxcentral"]];
[request addInputs:@[[SKData dataWithName:@"Days" andIntValue:days],
[SKData dataWithName:@"Provider" andIntValue:0],
[SKData dataWithName:@"Type" andStringValue:type]]
];
SKService *soapService = [[SKService alloc] init];
[soapService performRequest:request onSuccess:^(SKService *soapService, SKData *data) {
NSLog(@"Name: %@",data.name);
NSLog(@"Value: %@",data.stringValue);
} onFailure:^(SKService *soapService, NSError *error) {
NSLog(@"Error: %@",[error localizedDescription]);
}];
}
当您自己运行它时,您会看到结果是包含只是一个长字符串的 SKData *data
,它自身是以 XML 格式表示的,可以进行解析。
我们再次使用一个免费可用的 webservice(WSDL),它返回简单的但结构化的 XML,我们可直接使用。
- (void)loadItemsofIndustryType:(NSString *)type
onCompletion:(void (^)(NSArray *lectures, NSError *error))completion {
SKRequest *request = [[SKRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.webservicex.net/GenericNAICS.asmx"] operation:@"GetNAICSByIndustry" andNamespaceURL:[NSURL URLWithString:@"http://www.webservicex.net/"]];
[request addInput:[SKData dataWithName:@"IndustryName" andStringValue:type]];
SKService *soapService = [[SKService alloc] init];
[soapService performRequest:request onSuccess:^(SKService *soapService, SKData *data) {
SKDataObjectMapping *mapping = [SKDataObjectMapping mapperForClass:[NAICSItem class]];
NSArray *result = [mapping parseArray:[[data childByName:@"NAICSData"] childByName:@"NAICSData"].children];
NSLog(@"Got %lu results",(unsigned long)[result count]);
} onFailure:^(SKService *soapService, NSError *error) {
NSLog(@"Error: %@",[error localizedDescription]);
}];
}
现在,NSArray *result
包含了 NAICSItem
s 的结果数组。在这里,这是定义为一个简单领域对象类的 NAICSItem
@interface NAICSItem : NSObject
@property (nonatomic,strong)NSString *NAICSCode;
@property (nonatomic,strong)NSString *Title;
@property (nonatomic,strong)NSString *Country;
@property (nonatomic,strong)NSString *IndustryDescription;
@end
可能会有某种原因,您无法使用 XML 中的字段名称在领域对象中。幸运的是,这个问题已经在原始映射项目中解决了,所以也可以在这里使用。
- (void)loadItemsofIndustryType:(NSString *)type
onCompletion:(void (^)(NSArray *lectures, NSError *error))completion {
SKRequest *request = [[SKRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.webservicex.net/GenericNAICS.asmx"] operation:@"GetNAICSByIndustry" andNamespaceURL:[NSURL URLWithString:@"http://www.webservicex.net/"]];
[request addInput:[SKData dataWithName:@"IndustryName" andStringValue:type]];
SKService *soapService = [[SKService alloc] init];
[soapService performRequest:request onSuccess:^(SKService *soapService, SKData *data) {
SKParserConfiguration *configuration = [SKParserConfiguration configuration];
[configuration addObjectMapping:[SKObjectMapping mapKeyPath:@"Title" toAttribute:@"NAICSTitle" onClass:[NAICSItem class]]];
SKDataObjectMapping *mapping = [SKDataObjectMapping mapperForClass:[NAICSItem class]andConfiguration:configuration];
NSArray *result = [mapping parseArray:[[data childByName:@"NAICSData"] childByName:@"NAICSData"].children];
NSLog(@"Got %lu results",(unsigned long)[result count]);
for (id ritem in result) {
if ([ritem isKindOfClass:[NAICSItem class]]) {
[self.textView setText:[NSString stringWithFormat:@"%@ TITLE:%@\n DESCRIPTION:%@\n",self.textView.text, ((NAICSItem *)ritem).NAICSTitle, ((NAICSItem *)ritem).IndustryDescription]];
}
}
} onFailure:^(SKService *soapService, NSError *error) {
NSLog(@"Error: %@",[error localizedDescription]);
}];
}
这意味着当然,领域类中的字段需要从 Title
更改为 NAICSTitle
...是非常受欢迎的。当前实现的功能集合是最基本的,能满足我目前项目的需求,并且会随着我个人的需求增长而扩展。这就是我决定将其开源的原因,以便能够在更短的时间内涵盖更多用例。
此项目的源代码在标准MIT许可证下可用。请参阅许可证文件。