Comments (9)
@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.
@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)
}
}`
from xmlmapper.
@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.
@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:
can this issue be related to the fact that there is an xml version encoding utf8 header in front of the response?
thanks,
Patrick
from xmlmapper.
@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.
@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.
@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.
@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.
Closing this since question have been answered and there is no issue.
from xmlmapper.
Related Issues (20)
- Body is located before Header (Unordered tags) HOT 5
- Cannot correctly parse elements with innerText and optional attributes HOT 5
- Parse failing when illegal characters are present HOT 1
- Getting nil for array of elements HOT 2
- Save XML data to UserDefaults HOT 11
- Invalid XML string for tags with XML encoded values HOT 1
- Fix swift 5 warnings please. HOT 1
- Alamofire incompatability? HOT 2
- Ordered XML Attributes HOT 3
- Issue with optional attributes in simple tags
- Update to Alamofire 5 HOT 4
- Request subspec on Alamofire 4.9 HOT 2
- foobar2000 returns artist twice - once with just text, second time with attribute - XMLMapper can't access second array item HOT 5
- Question regarding dots in value HOT 2
- Recursive XML elements HOT 6
- How can I use Requests classes with Swift Package Manager? HOT 1
- Duplicate tag but different data HOT 4
- Use XMLMapper to render the exact nodes order for an XML file as they appear? HOT 6
- Privacy Manifest HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xmlmapper.