在Swift中使用Alamofire处理XML数据 [英] Handling XML data with Alamofire in Swift

查看:527
本文介绍了在Swift中使用Alamofire处理XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在当前的ios项目中使用cocoapods。我需要使用SOAP以简单的方式为我的ios项目获取内容。我用谷歌搜索了它,Alamofire pod对我来说很棒。因为我使用的是Swift编程语言。

I started to use cocoapods with my current ios project. I need to use SOAP to get content with easy way for my ios project. I have googled it and Alamofire pod is great for me. Because I am using Swift programming language.

我很容易使用这个pod。但我的Web服务返回XML结果。我想序列化以对此XML结果进行排序。但我不能。

I have inited easily this pod. But my web services return me XML result. And I want to serialisation to array this XML result. But I can't.

当我使用浏览器调用我的网络服务时,我得到了这种结果

When I call my web service with a browser I get this kind of result

Alamofire响应方法是像这样:

Alamofire response method is like this:

Alamofire.request(.GET, "http://my-web-service-domain.com", parameters: nil)
         .response { (request, response, data, error) in
                     println(request)
                     println(response)
                     println(error)
                   }

当我运行此方法时,我在终端上看到此输出:

When I run this method I see this output on the terminal:

<NSMutableURLRequest: 0x170010a30> { URL: http://my-web-service-domain.com }
Optional(<NSHTTPURLResponse: 0x1704276c0> { URL: http://my-web-service-domain.com } { status code: 200, headers {
    "Cache-Control" = "private, max-age=0";
    "Content-Length" = 1020;
    "Content-Type" = "text/xml; charset=utf-8";
    Date = "Thu, 18 Jun 2015 10:57:07 GMT";
    Server = "Microsoft-IIS/7.5";
    "X-AspNet-Version" = "2.0.50727";
    "X-Powered-By" = "ASP.NET";
} })
nil

我想将结果发送到一个数组,该数组在浏览器上显示我的故事板。
有人可以帮我如何使用Alamofire框架或Swift语言序列化这些数据吗?

I want to get result to an array which see on browser to show my storyboard. Can anybody help me how to serialise this data with Alamofire framework or Swift language?

推荐答案

如果我没有误会你的描述,我想你想得到XML数据并解析它,对吧?对此,您可以在响应回调中处理错误的变量。您应该 println(data)来检查XML文档。

If I did not misunderstand your description, I think you would like to get the XML data and parse it, right? Regarding to this, you may handle with wrong variables in the response callback. You should println(data) to check the XML document.

对于解析XML数据,您可以考虑 SWXMLHash 。 Alamofire请求可能如下所示:

For parsing XML data, you could consider SWXMLHash. The Alamofire request could look like:

Alamofire.request(.GET, "http://my-web-service-domain.com", parameters: nil)
         .response { (request, response, data, error) in
            println(data) // if you want to check XML data in debug window.
            var xml = SWXMLHash.parse(data!)
            println(xml["UserDTO"]["FilmID"].element?.text) // output the FilmID element.
         }

有关XML管理的更多信息,请检查SWXMLHash

Further information about XML management, please check SWXMLHash.

这篇关于在Swift中使用Alamofire处理XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆