Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse XML web service in Swift?

I don't know how to parse in my below code.. Can someone

func callService(usr: String, pwdCode: String) {

    let url = NSURL(string: "http://inspect.dev.cbre.eu/SyncServices/api/jobmanagement/PlusContactAuthenticationPost")
    var xmlParse:NSString  = ""
    var data : NSData!
    let request = NSMutableURLRequest(URL: url!)
    request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
     request.HTTPMethod = "POST"
    let dictionary = ["email": usr, "userPwd": pwdCode]
    var error: NSError?
    if let body = NSJSONSerialization.dataWithJSONObject(dictionary, options: nil, error: &error) {
        request.HTTPBody = body
    } else {
        println("JSON error: \(error)")
    }

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        (data, response, error) in
        println(NSString(data: data, encoding: NSUTF8StringEncoding))

        // xmlParse=NSString(data: data, encoding: NSUTF8StringEncoding)!
        // let data = (xmlParse as NSString).dataUsingEncoding(NSUTF8StringEncoding)
        // NSXMLParser(data : NSData)

        // xmlParse=NSString(data: data, encoding: NSUTF8StringEncoding)!
        // xmlParse=response
        // println(xmlParse)
    }
    task.resume()

}
like image 733
dhaval shah Avatar asked Nov 23 '14 08:11

dhaval shah


People also ask

What is XML parser in iOS Swift?

An XMLParser notifies its delegate about the items (elements, attributes, CDATA blocks, comments, and so on) that it encounters as it processes an XML document.

What is XML parser in Web technology?

XML parsing is the process of reading an XML document and providing an interface to the user application for accessing the document. An XML parser is a software apparatus that accomplishes such tasks.

What does parse XML activity do?

Parse XML is a synchronous activity that takes a binary XML file or an XML string and converts it into an XML schema tree based on the XSD specified.

Does SwiftUI use XML?

This is a great help in slowly transitioning an app to SwiftUI. Another benefit of SwiftUI focuses entirely on the workflow of developers. Up until now, UIs for apps were built with Interface Builder. Internally, Interface Builder uses an XML-based file format.


1 Answers

For anyone still looking, here is the code I used that worked quite well to convert the xml response into Dictionaries/Arrays, thanks to the SWXMLHash class...

UPDATED SWIFT 2.0

    let baseUrl = "http://www.example.com/file.xml"
    let request = NSMutableURLRequest(URL: NSURL(string: baseUrl)!)
    let session = NSURLSession.sharedSession()
    request.HTTPMethod = "GET"

    var err: NSError?

    let task = session.dataTaskWithRequest(request) {
        (data, response, error) in

        if data == nil {
            print("dataTaskWithRequest error: \(error)")
            return
        }

        let xml = SWXMLHash.parse(data)

        if let definition = xml["entry_list"]["entry"][0]["def"].element?.text {
            // ...
        }

        dispatch_async(dispatch_get_main_queue(),{
            // use main thread for UI updates
        })

    }
    task.resume()
like image 138
Tim S Avatar answered Sep 19 '22 12:09

Tim S