0
votes

Using Alamofire 3.1.0 and Swift 2

I'm trying to parse XML, but I get the following error

Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo={NSUnderlyingError=0x7f83f9449d50 {Error Domain=kCFErrorDomainCFNetwork Code=-1017 "(null)" UserInfo={_kCFStreamErrorCodeKey=-1, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey="same as NSErrorFailingURLKey", NSErrorFailingURLKey=http://webservices.ns.nl/ns-api-storingen, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1, NSLocalizedDescription=cannot parse response}

My GET function

private func GET(parameters: [String : String]) {
    Alamofire.request(.GET, URL, parameters: parameters, encoding: .PropertyList(.XMLFormat_v1_0, 0))
        .authenticate(user: user, password: password)
        .responsePropertyList { response in
            if let error = response.result.error {
                print(error)
            } else if let array = response.result.value as? [[String:String]] {
                if array.isEmpty {
                    print("No data")
                } else {
                    // do stuff
                }
            }
    }
}

This is the server response

Can someone tell me what I'm doing wrong?

1
Looks ok. At this point I would check what raw response I get from the server before trying to parse it, it could be different from what is expected. - Eric Aya
I've added the server response in my OP. I'm not sure if this is what you meant? - Thien
Yes, that was my intention in the comment, I thought maybe your XML wasn't valid. Thanks for editing your question. - Eric Aya

1 Answers

0
votes

I got it working using the following code (using SWXMLHash)

private func GET(parameters: [String : String]) {
    Alamofire.request(.GET, URL, parameters: parameters)
        .authenticate(user: user, password: password)
        .responseString { response in
            let xml = SWXMLHash.parse(response.result.value!)
            print(xml["Storingen"]["Ongepland"]["Storing"]["Traject"].element!.text!)
                }
}