Handling XML data with Alamofire in Swift Handling XML data with Alamofire in Swift ios ios

Handling XML data with Alamofire in Swift


If I did not misunderstand your description, I think you would like to get the XML data and parse it, right? Regarding to this, you may handle with wrong variables in the response callback. You should println(data) to check the XML document.

For parsing XML data, you could consider SWXMLHash. The Alamofire request could look like:

Alamofire.request(.GET, "http://my-web-service-domain.com", parameters: nil)         .response { (request, response, data, error) in            println(data) // if you want to check XML data in debug window.            var xml = SWXMLHash.parse(data!)            println(xml["UserDTO"]["FilmID"].element?.text) // output the FilmID element.         }

Further information about XML management, please check SWXMLHash.


Alamofire 4.x - Swift 3.x:

(please note that in this example I've used URLEncoding.default instead of URLEncoding.xml because the xml parameter exclude the possibility to pass parameters and headers, so default is more confortable.)

let url = "https://httpbin.org/get"let parameters: Parameters = ["foo": "bar"]let headers: HTTPHeaders = [    "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",    "Accept": "application/json"]Alamofire.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseString { response in    print(" - API url: \(String(describing: response.request!))")   // original url request    var statusCode = response.response?.statusCode    switch response.result {    case .success:        print("status code is: \(String(describing: statusCode))")        if let string = response.result.value {            print("XML: \(string)")        }    case .failure(let error):        statusCode = error._code // statusCode private        print("status code is: \(String(describing: statusCode))")        print(error)    }}

Alamofire 3.0 october 2015 and Xcode 7 according to the 3.0.0-beta.3 README and the Alamofire 3.0 Migration Guide.

For me the correct syntax is:

Alamofire.request(.GET, url, parameters: params, encoding: ParameterEncoding.URL).responsePropertyList { response in            if let error = response.result.error {                print("Error: \(error)")                // parsing the data to an array            } else if let array = response.result.value as? [[String: String]] {                if array.isEmpty {                    print("No data")                } else {                     //Do whatever you want to do with the array here                }            }        }

If you want a good XML parser, please take a look to SWXMLHash.

An example could be: let xml = SWXMLHash.parse(string)


Using Alamofire 3.0 current version as of Sept 2015 and Xcode 7.

The implementation bellow has the advantage of not using an additional external library such as SWXMLHash

Alamofire.request(.GET, urlString, encoding: .PropertyList(.XMLFormat_v1_0, 0)).responsePropertyList { request, response, result in//Note that result have two properties: error and value as of Alamofire 3.0, check the migration guide for more info  if let error = result.error {    print("Error: \(error)")    // parsing the data to an array   } else if let array = result.value as? [[String: String]] {    if array.isEmpty {      print("No data")    } else {       //Do whatever you want to do with the array here    }  }}