Swift XMLMapper解码嵌套属性

时间:2018-09-04 09:41:58

标签: ios iphone swift xml xmlmapper

使用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())
    }
}

在解码时,

  

安全(状态代码:无,状态:无,操作类型:无,网址:   可选(URLInfo(URL:可选(https://example.com),方法:   可选(“ POST”),contentType:   Optional(“ application / x-www-form-urlencoded”),typeId:Optional(1))),   hiddenFields:可选([[__name]:“ hidden-fields”,“ apiRequest”:   “” MIAGCSqGSIb3DQEHA6CAMIACAQAxggFAMIIBPAIBAD“]),返回网址:   可选(https://example.com

statusstatusCodeactionType怎么了?为什么不解码,是因为无法进行深度嵌套的解码?

1 个答案:

答案 0 :(得分:2)

我希望我的图书馆能够那样工作。 (嵌套值的映射非常干净),但是目前很难映射嵌套属性和内部具有其他元素或属性的元素的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())
    }
}

两个模型都可以正常工作。

如果嵌套映射在下一版本中得到改善,我一定会更新此帖子。