2016-08-10 55 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首先,現在嘗試,我做了一些改變。其次,你不應該指望你給你的代碼來處理你的項目。拿這個代碼來玩,嘗試,改變,學習,這樣你就會成爲一個更好的開發者。 :) –