使用SWXMLHash反序列化xml时处理缺少的属性

时间:2021-07-01 15:27:38

I've been following the examples at SWXMLHash to deserialize an XML file. It is working quite well, but I am not sure how to handle cases when the XML input is incomplete:

我一直在关注SWXMLHash中的示例来反序列化XML文件。它工作得很好,但我不确定如何在XML输入不完整时处理案例:

For example, suppose the XML input is this:

例如,假设XML输入是这样的:

<shippingInfo>
            <shippingServiceCost currencyId="USD">0.0</shippingServiceCost>
            <shippingType>Free</shippingType>
            <shipToLocations>US</shipToLocations>
            <expeditedShipping>true</expeditedShipping>
            <oneDayShippingAvailable>false</oneDayShippingAvailable>
            <handlingTime>1</handlingTime>
</shippingInfo>

To deserialize this XML, I create the following struct that is an XMLIndexerDeserializable

为了反序列化这个XML,我创建了以下XMLIndexerDeserializable结构

import SWXMLHash

struct ShippingInfo: XMLIndexerDeserializable
{
    let currencyId: String
    let shippingServiceCost: Double
    let shippingType: String
    let shipToLocations: String
    let expeditedShipping: Bool
    let oneDayShippingAvailable: Bool
    let handlingTime: Int

    static func deserialize(_ node: XMLIndexer) throws -> ShippingInfo 
    {
        return try ShippingInfo(
            currencyId: node["shippingServiceCost"].value(ofAttribute: "currencyId"),
            shippingServiceCost: node["shippingServiceCost"].value(),
            shippingType: node["shippingType"].value(),
            shipToLocations: node["shipToLocations"].value(),
            expeditedShipping: node["expeditedShipping"].value(),
            oneDayShippingAvailable: node["oneDayShippingAvailable"].value(),
            handlingTime: node["handlingTime"].value()
        )
    }
}

The code above works, until the shippingInfo XML misses an element, like so:

上面的代码有效,直到shippingInfo XML错过了一个元素,如下所示:

<shippingInfo>
            <shippingServiceCost currencyId="USD">0.0</shippingServiceCost>
            <shippingType>Free</shippingType>
            <shipToLocations>Worldwide</shipToLocations>
            <expeditedShipping>false</expeditedShipping>
            <oneDayShippingAvailable>false</oneDayShippingAvailable>
</shippingInfo>

The 2nd XML above is missing the property "handlingTime". Running the deserialization code above would throw an exception at node["handlingTime"].value()

上面的第二个XML缺少属性“handlingTime”。运行上面的反序列化代码会在node [“handlingTime”]处抛出异常.value()

One approach to fix this problem is to try to catch the exception whenever we access the XMLIndexer's key, and pass in a default value to the property if the exception is thrown which means the key isn't there. I don't think this is best approach though.

解决此问题的一种方法是尝试在访问XMLIndexer的密钥时捕获异常,并在抛出异常时将默认值传递给属性,这意味着密钥不存在。我不认为这是最好的方法。

What is the best approach to deserialize XML when the XML is missing properties?

当XML缺少属性时,反序列化XML的最佳方法是什么?

1 个解决方案

#1


1  

Change the declaration of your handlingTime property from Int to Int?, like so:

将handleTime属性的声明从Int更改为Int ?,如下所示:

let handlingTime: Int?

It needs to be nullable so that the deserialization can support a value that doesn't exist.

它需要是可空的,以便反序列化可以支持不存在的值。

Hope this helps!

希望这可以帮助!

#1


1  

Change the declaration of your handlingTime property from Int to Int?, like so:

将handleTime属性的声明从Int更改为Int ?,如下所示:

let handlingTime: Int?

It needs to be nullable so that the deserialization can support a value that doesn't exist.

它需要是可空的,以便反序列化可以支持不存在的值。

Hope this helps!

希望这可以帮助!