Swift XMLMapper 解码嵌套属性
Swift XMLMapper decoding nested attributes
使用 here 中的库。这是详细信息,
XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<status code="25">Verification required</status>
<parsed-challenge>
<action type-id="11">
<url content-type="application/x-www-form-urlencoded" method="POST" type-id="1">https://example.com</url>
<hidden-fields>
<apiRequest>MIAGCSqGSIb3DQEHA6CAMIACAQAxggFAMIIBPAIBAD</apiRequest>
</hidden-fields>
</action>
</parsed-challenge>
<return-url>https://example.com</return-url>
</root>
结构:
struct Secure: XMLMappable {
internal(set) var statusCode: Int?
internal(set) var status: String?
internal(set) var actionType: Int?
internal(set) var url: URLInfo?
internal(set) var hiddenFields: [String:String]?
internal(set) var returnURL: Foundation.URL?
public var nodeName: String!
public init(map: XMLMap) {
statusCode = map.value()
status = map.value()
actionType = map.value()
url = map.value()
hiddenFields = map.value()
returnURL = map.value()
}
public mutating func mapping(map: XMLMap) {
statusCode <- map["status"].attributes["code"]
status <- map["status"].innerText
actionType <- map["parsed-challenge.action"].attributes["type-id"]
url <- map["parsed-challenge.action.url"]
hiddenFields <- map["parsed-challenge.action.hidden-fields"]
returnURL <- (map["return-url"], XMLURLTransform())
}
}
解码时,
Secure(statusCode: nil, status: nil, actionType: nil, url:
Optional(URLInfo(url: Optional(https://example.com), method:
Optional("POST"), contentType:
Optional("application/x-www-form-urlencoded"), typeId: Optional(1))),
hiddenFields: Optional(["__name": "hidden-fields", "apiRequest":
"MIAGCSqGSIb3DQEHA6CAMIACAQAxggFAMIIBPAIBAD"]), returnURL:
Optional(https://example.com))
status
、statusCode
和 actionType
有什么问题?为什么他们不解码,是不是因为深度嵌套解码是不可能的?
我希望我的图书馆能够像那样工作。 (这样嵌套值的映射非常干净)但是目前很难映射嵌套属性和 innerText
内部具有其他元素或属性的元素。
因此,映射您的 XML 的建议模型如下所示:
struct Secure: XMLMappable {
public var nodeName: String!
internal(set) var status: Status?
internal(set) var action: Action?
internal(set) var returnURL: Foundation.URL?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
status <- map["status"]
action <- map["parsed-challenge.action"]
returnURL <- (map["return-url"], XMLURLTransform())
}
}
struct Status: XMLMappable {
public var nodeName: String!
internal(set) var statusCode: Int?
internal(set) var message: String?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
statusCode <- map.attributes["code"]
message <- map.innerText
}
}
struct Action: XMLMappable {
public var nodeName: String!
internal(set) var actionType: Int?
internal(set) var url: URLInfo?
internal(set) var hiddenFields: [String:String]?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
actionType <- map.attributes["type-id"]
url <- map["url"]
hiddenFields <- map["hidden-fields"]
}
}
struct URLInfo: XMLMappable {
public var nodeName: String!
internal(set) var contentType: String?
internal(set) var method: String?
internal(set) var typeID: Int?
internal(set) var url: Foundation.URL?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
contentType <- map.attributes["content-type"]
method <- map.attributes["method"]
typeID <- map.attributes["type-id"]
url <- (map.innerText, XMLURLTransform())
}
}
虽然我个人使用(因为我知道如何破解我的图书馆)我可以使用这样的东西:
struct Secure: XMLMappable {
public var nodeName: String!
internal(set) var statusCode: Int?
internal(set) var status: String?
internal(set) var actionType: Int?
internal(set) var url: URLInfo?
internal(set) var hiddenFields: [String:String]?
internal(set) var returnURL: Foundation.URL?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
statusCode <- map["status._code"]
status <- map["status.__text"]
actionType <- map["parsed-challenge.action._type-id"]
url <- map["parsed-challenge.action.url"]
hiddenFields <- map["parsed-challenge.action.hidden-fields"]
returnURL <- (map["return-url"], XMLURLTransform())
}
}
struct URLInfo: XMLMappable {
public var nodeName: String!
internal(set) var contentType: String?
internal(set) var method: String?
internal(set) var typeID: Int?
internal(set) var url: Foundation.URL?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
contentType <- map.attributes["content-type"]
method <- map.attributes["method"]
typeID <- map.attributes["type-id"]
url <- (map.innerText, XMLURLTransform())
}
}
两个模型都可以正常工作。
如果嵌套映射在下一个版本中得到改进,我一定会更新这个post。
使用 here 中的库。这是详细信息,
XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<status code="25">Verification required</status>
<parsed-challenge>
<action type-id="11">
<url content-type="application/x-www-form-urlencoded" method="POST" type-id="1">https://example.com</url>
<hidden-fields>
<apiRequest>MIAGCSqGSIb3DQEHA6CAMIACAQAxggFAMIIBPAIBAD</apiRequest>
</hidden-fields>
</action>
</parsed-challenge>
<return-url>https://example.com</return-url>
</root>
结构:
struct Secure: XMLMappable {
internal(set) var statusCode: Int?
internal(set) var status: String?
internal(set) var actionType: Int?
internal(set) var url: URLInfo?
internal(set) var hiddenFields: [String:String]?
internal(set) var returnURL: Foundation.URL?
public var nodeName: String!
public init(map: XMLMap) {
statusCode = map.value()
status = map.value()
actionType = map.value()
url = map.value()
hiddenFields = map.value()
returnURL = map.value()
}
public mutating func mapping(map: XMLMap) {
statusCode <- map["status"].attributes["code"]
status <- map["status"].innerText
actionType <- map["parsed-challenge.action"].attributes["type-id"]
url <- map["parsed-challenge.action.url"]
hiddenFields <- map["parsed-challenge.action.hidden-fields"]
returnURL <- (map["return-url"], XMLURLTransform())
}
}
解码时,
Secure(statusCode: nil, status: nil, actionType: nil, url: Optional(URLInfo(url: Optional(https://example.com), method: Optional("POST"), contentType: Optional("application/x-www-form-urlencoded"), typeId: Optional(1))), hiddenFields: Optional(["__name": "hidden-fields", "apiRequest": "MIAGCSqGSIb3DQEHA6CAMIACAQAxggFAMIIBPAIBAD"]), returnURL: Optional(https://example.com))
status
、statusCode
和 actionType
有什么问题?为什么他们不解码,是不是因为深度嵌套解码是不可能的?
我希望我的图书馆能够像那样工作。 (这样嵌套值的映射非常干净)但是目前很难映射嵌套属性和 innerText
内部具有其他元素或属性的元素。
因此,映射您的 XML 的建议模型如下所示:
struct Secure: XMLMappable {
public var nodeName: String!
internal(set) var status: Status?
internal(set) var action: Action?
internal(set) var returnURL: Foundation.URL?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
status <- map["status"]
action <- map["parsed-challenge.action"]
returnURL <- (map["return-url"], XMLURLTransform())
}
}
struct Status: XMLMappable {
public var nodeName: String!
internal(set) var statusCode: Int?
internal(set) var message: String?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
statusCode <- map.attributes["code"]
message <- map.innerText
}
}
struct Action: XMLMappable {
public var nodeName: String!
internal(set) var actionType: Int?
internal(set) var url: URLInfo?
internal(set) var hiddenFields: [String:String]?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
actionType <- map.attributes["type-id"]
url <- map["url"]
hiddenFields <- map["hidden-fields"]
}
}
struct URLInfo: XMLMappable {
public var nodeName: String!
internal(set) var contentType: String?
internal(set) var method: String?
internal(set) var typeID: Int?
internal(set) var url: Foundation.URL?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
contentType <- map.attributes["content-type"]
method <- map.attributes["method"]
typeID <- map.attributes["type-id"]
url <- (map.innerText, XMLURLTransform())
}
}
虽然我个人使用(因为我知道如何破解我的图书馆)我可以使用这样的东西:
struct Secure: XMLMappable {
public var nodeName: String!
internal(set) var statusCode: Int?
internal(set) var status: String?
internal(set) var actionType: Int?
internal(set) var url: URLInfo?
internal(set) var hiddenFields: [String:String]?
internal(set) var returnURL: Foundation.URL?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
statusCode <- map["status._code"]
status <- map["status.__text"]
actionType <- map["parsed-challenge.action._type-id"]
url <- map["parsed-challenge.action.url"]
hiddenFields <- map["parsed-challenge.action.hidden-fields"]
returnURL <- (map["return-url"], XMLURLTransform())
}
}
struct URLInfo: XMLMappable {
public var nodeName: String!
internal(set) var contentType: String?
internal(set) var method: String?
internal(set) var typeID: Int?
internal(set) var url: Foundation.URL?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
contentType <- map.attributes["content-type"]
method <- map.attributes["method"]
typeID <- map.attributes["type-id"]
url <- (map.innerText, XMLURLTransform())
}
}
两个模型都可以正常工作。
如果嵌套映射在下一个版本中得到改进,我一定会更新这个post。