Code Monkey home page Code Monkey logo

Comments (9)

gcharita avatar gcharita commented on June 12, 2024

@PatrickB100 since you want to map your response XML to an array directly you need to use the responseXMLArray(queue:keyPath:dataPreprocessor:encoding:emptyResponseCodes:emptyRequestMethods:options:completionHandler:)
function instead of responseXMLObject(queue:keyPath:dataPreprocessor:mapToObject:encoding:emptyResponseCodes:emptyRequestMethods:options:completionHandler:) that you currently using.

With that modification your code will pass compilation:

AF.request(RequestUrl, method: .post, parameters: getallpersonsquery.toXML(), encoding: XMLEncoding.default, headers: headers)
    .redirect(using: redirector)
    .responseXMLArray { (dataResponse: DataResponse<[AllCnannelModel], AFError>) in

    }

from xmlmapper.

PatrickB100 avatar PatrickB100 commented on June 12, 2024

@gcharita thanks! that made me some progress. Now i'm getting the following return: Cannot use optional chaining on non-optional value of type '[CDCatalog]'
and Value of type '[CDCatalog]' has no member 'cds'

find the code below. Do you have any suggestions?

thanks in advance,

Patrick

`class CDCatalog: XMLMappable {
var nodeName: String!

        var cds: [CD]?

        required init?(map: XMLMap) {}

        func mapping(map: XMLMap) {
            cds <- map["ROW"]
        }
    }

    class CD: XMLMappable {
        var nodeName: String!

        var title: String!


        required init?(map: XMLMap) {}

        func mapping(map: XMLMap) {
            title <- map["NAME"]
        }
    }
    
    AF.request(RequestUrl, method: .post, parameters: updateperson.toXML(), encoding: XMLEncoding.default)
        .redirect(using: redirector)
        .responseXMLArray { (response: DataResponse<[CDCatalog], AFError>) in
            
            
            switch response.result {
            
            case .success(let value):
                print("AF-Success")
                
                print(value?.cds?.first.title ?? "nil")
                
                


            case .failure(let error):
                print("AF-Error")
                print(error)

            }
        }`

image

from xmlmapper.

gcharita avatar gcharita commented on June 12, 2024

@PatrickB100 well, the error is obvious. You are trying to access cds property of CDCatalog, but your value is of type [CDCatalog] (array of CDCatalog objects).

It looks like you are trying to run the README example. If that's the case then you were correct before about using responseXMLObject(queue:keyPath:dataPreprocessor:mapToObject:encoding:emptyResponseCodes:emptyRequestMethods:options:completionHandler:) function, but you need to change the type to CDCatalog instead of [CDCatalog]:

AF.request(RequestUrl, method: .post, parameters: updateperson.toXML(), encoding: XMLEncoding.default)
    .redirect(using: redirector)
    .responseXMLObject { (response: DataResponse<CDCatalog, AFError>) in
        switch response.result {
        case .success(let value):
            print("AF-Success")
            print(value.cds?.first?.title ?? "nil")
        case .failure(let error):
            print("AF-Error")
            print(error)
        }
    }

Generally, which of the 2 function you have to choose depends on the XML that your API will respond to you.

from xmlmapper.

PatrickB100 avatar PatrickB100 commented on June 12, 2024

@gcharita thanks for your support! i'm indeed following the readme example.

i'm getting a reponse in the debug area where it states the following:
*Result]: success(testApp.PersonDetailsView.(unknown context at $102f51890).(unknown context at $102f519cc).CDCatalog)

the returned body seems to be correct:

image

can this issue be related to the fact that there is an xml version encoding utf8 header in front of the response?

thanks,

Patrick

image

from xmlmapper.

gcharita avatar gcharita commented on June 12, 2024

@PatrickB100 this response has nothing to do with the README example. You need to create your own model. The one that matches with your response XML.

from xmlmapper.

gcharita avatar gcharita commented on June 12, 2024

@PatrickB100 a possible model that you can use to map your XML is this:

class Result: XMLMappable {
    var nodeName: String!

    var error: String?
    var rowset: Rowset?

    required init?(map: XMLMap) {}

    func mapping(map: XMLMap) {
        error <- map.attributes["error"]
        rowset <- map["ROWSET"]
    }
}

class Rowset: XMLMappable {
    var nodeName: String!

    var rows: [Row]?

    required init?(map: XMLMap) {}

    func mapping(map: XMLMap) {
        rows <- map["ROW"]
    }
}

class Row: XMLMappable {
    var nodeName: String!

    var name: String?
    var prefix: String?
    var firstName: String?
    var homeAddress: String?
    var homeZip: String?
    var homeCity: String?

    required init?(map: XMLMap) {}

    func mapping(map: XMLMap) {
        name <- map["NAME"]
        prefix <- map["PREFIX"]
        firstName <- map["FIRSTNAME"]
        homeAddress <- map["HOMEADDRESS"]
        homeZip <- map["HOMEZIP"]
        homeCity <- map["HOMECITY"]
    }
}

Calling your API with Alamofire may look like this:

AF.request(RequestUrl, method: .post, parameters: updateperson.toXML(), encoding: XMLEncoding.default)
    .redirect(using: redirector)
    .responseXMLObject { (response: DataResponse<Result, AFError>) in
        switch response.result {
        case .success(let value):
            print("AF-Success")
            print(value.rowset?.rows?.first?.name ?? "nil")
        case .failure(let error):
            print("AF-Error")
            print(error)
        }
    }

from xmlmapper.

PatrickB100 avatar PatrickB100 commented on June 12, 2024

@gcharita Thank you very much! it's working now. Do you know if there is a README on how to put reponse data from XMLMapper into an UITableView?

Patrick

from xmlmapper.

gcharita avatar gcharita commented on June 12, 2024

@PatrickB100 you need to use rows array to do that and follow Apple's official Filling a Table with Data documentation. You can also find other great posts online that match your particular case.

from xmlmapper.

gcharita avatar gcharita commented on June 12, 2024

Closing this since question have been answered and there is no issue.

from xmlmapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.