Search code examples
swiftswxmlhash

How should i save the xml child element value? [swift]


i have this set of xml that i needed to save the category of price_options, but how do i save it? For now i using SWXMLHash, but i don't know how to save it:

<rss version="2.0">
<channel>
    <timestamp>2015-02-27 18:27:21</timestamp>
    <tot_record>66</tot_record>
    <record>66</record>
    <category>NO</category>
    <item>
        <sku>JC-0000000000594</sku>
        <seller>LT Stationery Sdn Bhd</seller>
        <seller_logo></seller_logo>
        <seller_logo_thumb></seller_logo_thumb>
        <name>128PG Foolscap Book -3 Column-HC25511 (RED)</name>
        <products_cat>118</products_cat>
        <description></description>
        <delivery_time>1-2 business days</delivery_time>
        <img_1>http://dev.jocom.com.my/images/data/594-img1.jpg</img_1>
        <thumb_1>http://dev.jocom.com.my/images/data/thumbs/594-img1.jpg</thumb_1>
        <img_2></img_2>
        <thumb_2></thumb_2>
        <img_3></img_3>
        <thumb_3></thumb_3>
        <vid_1></vid_1>
        <qr_code>JC594</qr_code>
        <zone_records>1</zone_records>
        <delivery_zones>
                <zone>3</zone>
        </delivery_zones>
        <price_records>1</price_records>
        <price_options>
            <option>
                <id>622</id>
                <label>128PG Foolscap Book -3 Column-HC25511 (RED)-BK-3C1200</label>
                <price>8.90</price>
                <promo_price>0</promo_price>
                <qty>10</qty>
                <default>TRUE</default>
            </option>
        </price_options>
    </item>

And here is the code that i try to working on, but its not working, how should i save the child value? :

   func searchProducts(){

    var id = [String]()
    var price = [String]()


    let urlString = "http://dev.jocom.com.my/feed"


    Alamofire.request(.POST, urlString , parameters: ["req" : "pro_name", "code" : searchString!])

        .responseData { response in

            switch response.result {
            case .Success:

                let apiSearchXML = SWXMLHash.parse(response.data!)
                print(apiSearchXML)

                for elem in apiSearchXML["rss"]["channel"]["item"]["price_options"]["option"] {
                    id.append(elem["id"].element!.text!)
                    print("\(id)")
                }



            case .Failure(let error):
                print(error)
            }
    }


}

Solution

  • You need to use something else, because SWXMLHash does not support writing (as of 19 July 2016):

    Does SWXMLHash support writing XML content?

    No, not at the moment - SWXMLHash only supports parsing XML (via indexing, deserialization, etc.).

    You can try AEXML which supports writing.