SWXMLMapping
SWXMLMapping is a light-weight Objective-C to XML serialization framework.
Example
A mapping file is used to describe the conversion from Objective-C objects to XML:
<?xml version="1.0"?> <mapping> <class name="PMProperty" tag="property"> <string tag="name" keyPath="name" /> </class> <class name="PMTenant" tag="tenant"> <string tag="name" keyPath="name" /> </class> <class name="PMTransaction" tag="transaction"> <date tag="receivedDate" keyPath="when" format="%Y-%m-%d"/> <date tag="processedOn" keyPath="processedOn" format="%Y-%m-%d"/> <string tag="receipt" keyPath="receipt" /> <string tag="what" keyPath="what" /> <number tag="amount" keyPath="amount" format="decimal" /> <boolean tag="adjustment" keyPath="adjustment" /> </class> <class name="PMTenure" tag="tenure"> <object tag="property" keyPath="property" /> <object tag="tenant" keyPath="tenant" /> <date tag="startDate" keyPath="startDate" format="%Y-%m-%d" /> <date tag="endDate" keyPath="endDate" format="%Y-%m-%d" /> <number tag="numberOfPeriods" keyPath="numberOfPeriods" format="integer"/> <number tag="numberOfDays" keyPath="numberOfDays" format="integer" /> <number tag="rentalAmount" keyPath="rentalAmount" format="decimal" /> <number tag="bondAmount" keyPath="bondAmount" format="decimal" /> <number tag="rentalPeriod" keyPath="rentalPeriod" format="integer" /> <boolean tag="chargePartialPeriods" keyPath="chargePartialPeriods" /> <collection tag="transactions" keyPath="transactions" /> </class> </mapping>
We then load this mapping file, and provide an object to serialize:
// Load the mapping from a given XML file. filePath = @".../Mapping.xml"; mapping = [SWXMLMapping mappingFromURL:[NSURL fileURLWithPath:filePath]]; // Convert the object hierarchy to a string containing XML formatted data. NSString * xml = [mapping serializeObject:root];
Follow Me