xml - 保存 xml 数据响应 [swift]

标签 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 库

最佳答案

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
            }
        }
    }
}

关于xml - 保存 xml 数据响应 [swift],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38866872/

相关文章:

java - Oracle xdb-xmlparser 源代码

ios - 读取 xml 文件时得到 NSCocoaErrorDomain code=261

ios - Swift 3 UITableViewDataSource 选择器

ios - 向下滑动 UIView 然后再向上滑动

c# - C#读取XML文件

xml - 用于清除值的功能区 XML 组合框控件... VB.NET

java - 带 Java validator 的 XSD "minOccurs"未按预期工作

ios - Dynamo DB 无法保存数据

swift - 如何去掉 UISearchBar 的边框?

ios - 更改特定页面 UiPageViewController