保存xml数据响应[swift]

时间:2016-08-10 07:29:41

标签: xml swift

这是我从api返回的xml响应:

<item>
        <sku>JC-0000000004780</sku>
        <name>Moco Black Bean Drink</name>
        <description></description>
        <delivery_time>24 hours</delivery_time>
        <img_1></img_1>
        <img_2></img_2>
        <img_3></img_3>
        <thumb_1></thumb_1>
        <thumb_2></thumb_2>
        <thumb_3></thumb_3>
        <vid_1></vid_1>
        <qr_code>JC4780</qr_code>
        <zone_records>1</zone_records>
        <delivery_zones>
                <zone>3</zone>
                <zone_name>Within Klang Valley</zone_name>
        </delivery_zones>
        <price_records>1</price_records>
        <price_option>
            <option>
                <id>9645</id>
                <label>190ml</label>
                <price>4.13</price>
                <promo_price>0</promo_price>
                <qty>50</qty>
                <stock>0.00</stock>
                <stock_unit></stock_unit>
                <default>TRUE</default>
                <p_weight>0</p_weight>
            </option>
        </price_option>
        <related_products></related_products>
        <freshness></freshness>
        <bulk>0</bulk>
        <halal>0</halal>
    </item>
    <item>
        <sku>JC-0000000004779</sku>
        <name>Hongcho Vinegar Drink Pomegranate </name>
        <description></description>
        <delivery_time>24 hours</delivery_time>
        <img_1></img_1>
        <img_2></img_2>
        <img_3></img_3>
        <thumb_1></thumb_1>
        <thumb_2></thumb_2>
        <thumb_3></thumb_3>
        <vid_1></vid_1>
        <qr_code>JC4779</qr_code>
        <zone_records>1</zone_records>
        <delivery_zones>
                <zone>3</zone>
                <zone_name>Within Klang Valley</zone_name>
        </delivery_zones>
        <price_records>2</price_records>
        <price_option>
            <option>
                <id>9696</id>
                <label>500ml</label>
                <price>23.50</price>
                <promo_price>21.37</promo_price>
                <qty>50</qty>
                <stock>0.00</stock>
                <stock_unit></stock_unit>
                <default>TRUE</default>
                <p_weight>0</p_weight>
            </option>
            <option>
                <id>9644</id>
                <label>900ml</label>
                <price>38.50</price>
                <promo_price>0</promo_price>
                <qty>50</qty>
                <stock>0.00</stock>
                <stock_unit></stock_unit>
                <default>FALSE</default>
                <p_weight>0</p_weight>
            </option>
        </price_option>
        <related_products></related_products>
        <freshness></freshness>
        <bulk>0</bulk>
        <halal>0</halal>
    </item>

棘手的问题是price_records部分,有些项目只会返回1个价格记录,有些会返回2个价格记录等等(作为我提供的回复),如何保存呢?我正在使用SWXMLHash库

1 个答案:

答案 0 :(得分:1)

let xml = SWXMLHash.parse(data)

for item in xml["item"] { // Iterate over items
    if let priceRecordsValue = item["price_records"].element?.text { // Check if price_records value exist
        let priceRecordsCount = Int(priceRecordsValue) ?? 1 // Keep amount of price records in form of Int, default is 1
        for index in 0...(priceRecordsCount-1) { // Iterate over the amount of price records
            if let optionElement = item["price_option"]["option"][index].element { // Check if option element[n] exists
                // Do something with option element[n] here
            }
        }
    }
}