2016-08-10 48 views
0

这是XML响应,我从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库

+0

使用SWXMLHash解析xml后,您需要遍历xml并相应地使用这些数据。例如,在迭代中,如果您看到price_records键持有n个price_options,则可以访问price_options n次以获取所有价格选项。 –

+0

你能告诉我如何? @RoyK – bobo

回答

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

当我调试时,它不会循环到“if let optionElement = item [”price_option“] [”options“] [index] .element {//检查选项元素[n]是否存在 //做一些与选项元素[n]在这里 }“ – bobo

+0

它不工作): – bobo

+1

@bobo首先,现在尝试,我做了一些改变。其次,你不应该指望你给你的代码来处理你的项目。拿这个代码来玩,尝试,改变,学习,这样你就会成为一个更好的开发者。 :) –