SAXy OX 是 Objective-C 的一个全面功能 XML 和 JSON打了包框架。它的目的是允许领域对象可以以最少的编码量序列化到 XML 或 JSON。
功能包括
SAXyTests 文件夹中有几个经过良好记录的例子,包括
使用提示
例如,给定以下类
@interface CartoonCharacter : NSObject
@property(nonatomic)NSString *firstName;
@property(nonatomic)NSString *lastName;
@end
可以在几行代码中定义 SAXy 映射器和读取器
NSString *xml = @"<tune><first>Daffy</first><last>Duck</last></tune>";
OXmlReader *reader = [OXmlReader readerWithMapper: //declares a reader with embedded mapper
[[OXmlMapper mapper] elements:@[
[OXmlElementMapper rootXPath:@"/tune" type:[CartoonCharacter class]]
,
[[[OXmlElementMapper elementClass:[CartoonCharacter class]]
xpath:@"first" property:@"firstName"]
xpath:@"last" property:@"lastName"]
]]
];
CartoonCharacter *tune = [reader readXmlText:xml]; //reads xml
STAssertEqualObjects(@"Daffy", tune.firstName, @"mapped 'first' element to 'firstName' property");
STAssertEqualObjects(@"Duck", tune.lastName, @"mapped 'last' element to 'lastName' property");