使用 XMLMapper,您可以生成此 XML。
尝试以下模型:
class Service: XMLMappable {
var nodeName: String! = "SERVICE"
var name: String?
var parameters: [Parameter]?
init(name: String?, parameters: [Parameter]?) {
self.name = name
self.parameters = parameters
}
required init?(map: XMLMap) {}
func mapping(map: XMLMap) {
name <- map.attributes["name"]
parameters <- map["PARAMETERS.PARAMETER"]
}
}
class Parameter: XMLMappable {
var nodeName: String!
var name: String?
var value: String?
init(name: String?, value: String?) {
self.name = name
self.value = value
}
required init?(map: XMLMap) {}
func mapping(map: XMLMap) {
name <- map.attributes["name"]
value <- map.innerText
}
}
生成您的 XML,如:
let service = Service(
name: "dataservice/transaction.getView",
parameters: [
Parameter(
name: "columnList",
value: "PERSON.FIRSTNAME,PERSON.PREFIX,PERSON.NAME,ACCESSKEY.RCN,READER.NAME"
),
Parameter(
name: "filterTransTypeList",
value: "1,2"
),
Parameter(
name: "fromDate",
value: "2021-01-17 00:00:00"
),
]
)
print(service.toXMLString() ?? "nil")
或与 Alamofire 一起发送(使用Requests
子规范):
AF.request("<URL>", method: .post, parameters: service.toXML(), encoding: XMLEncoding.default)