
[
](mailto:edwin@evict.nl?SUBJECT=About AlamofireXmlToObjects)
With AlamofireXmlToObjects it's extremely easy to fetch a XML feed and parse it into objects. No property mapping is required. Reflection is used to put the values in the corresponding properties.
AlamofireXmlToObjects is based on the folowing libraries:
- Alamofire is an elegant HTTP Networking library in Swift
- XMLDictionary Is a simple way to parse and generate XML. Converts an XML file to an NSDictionary
- EVReflection is used to parse the XML result to your objects
If you have a JSON service and want the same functionality then have a look at AlamofireJsonToObjects.
Run the tests to see AlamofireXmlToObjects in action.
'AlamofireXmlToObjects' is available through the dependency manager CocoaPods. You do have to use cocoapods version 0.36 or later
You can just add AlamofireXmlToObjects to your workspace by adding the folowing 2 lines to your Podfile:
use_frameworks!
pod "AlamofireXmlToObjects"
Version 0.36 of cocoapods will make a dynamic framework of all the pods that you use. Because of that it's only supported in iOS 8.0 or later. When using a framework, you also have to add an import at the top of your swift file like this:
import AlamofireXmlToObjects
If you want support for older versions than iOS 8.0, then you can also just copy the AlamofireXmlToObjects.swift to your project. (Also see the instructions for embedding the EVReflection, XMLDictionary and Alamofire libraries)
class WeatherResponse: EVObject {
var location: String?
var three_day_forecast: [Forecast] = [Forecast]()
}
class Forecast: EVObject {
var day: String?
var temperature: NSNumber?
var conditions: String?
}
class AlamofireXmlToObjectsTests {
func testResponseObject() {
let URL = "https://raw.githubusercontent.com/evermeer/AlamofireXmlToObjects/master/AlamofireXmlToObjectsTests/sample_xml"
Alamofire.request(.GET, URL, parameters: nil)
.responseObject { (response: WeatherResponse?, error: NSError?) in
// That was all... You now have a WeatherResponse object with data
}
}
}
The code above will parse the folowing XML into the objects:
<wheather>
<location>Toronto, Canada</location>
<three_day_forecast>
<forecast>
<conditions>Partly cloudy</conditions>
<day>Monday</day>
<temperature>20</temperature>
</forecast>
<forecast>
<conditions>Showers</conditions>
<day>Tuesday</day>
<temperature>22</temperature>
</forecast>
<forecast>
<conditions>Sunny</conditions>
<day>Wednesday</day>
<temperature>28</temperature>
</forecast>
</three_day_forecast>
</wheather>
AlamofireXmlToObjects is available under the MIT 3 license. See the LICENSE file for more info.
Also see my other open source iOS libraries:
- EVReflection - Swift library with reflection functions with support for NSCoding, Printable, Hashable, Equatable and JSON
- EVCloudKitDao - Simplified access to Apple's CloudKit
- EVFaceTracker - Calculate the distance and angle of your device with regards to your face in order to simulate a 3D effect
- EVURLCache - a NSURLCache subclass for handling all web requests that use NSURLReques
- AlamofireJsonToObject - An Alamofire extension which converts JSON response data into swift objects using EVReflection
- AlamofireXmlToObject - An Alamofire extension which converts XML response data into swift objects using EVReflection and XMLDictionary
- AlamofireOauth2 - A swift implementation of OAuth2 using Alamofire
- EVWordPressAPI - Swift Implementation of the WordPress (Jetpack) API using AlamofireOauth2, AlomofireXmlToObjects and EVReflection (work in progress)
- PassportScanner - Scan the MRZ code of a passport and extract the firstname, lastname, passport number, nationality, date of birth, expiration date and personal numer.