快速进行XML解析 [英] XML parsing in swift

查看:92
本文介绍了快速进行XML解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能帮我为什么下面的代码不起作用吗?我正在Xcode.1 Playground中对其进行测试

Can some body help me why the below code is not working.. I am testing it in Xcode.1 Playground

let url:NSURL! = NSURL(fileURLWithPath:"file:///Users/sss/Documents/app.xml")

var xml = NSXMLParser(contentsOfURL: url)

xml?.parse()

推荐答案

操场被沙盒化,因此您将无法仅从用户文件夹中的任何位置抓取文件.将文件添加到游乐场以使其可访问的方法如下:

Playgrounds are sandboxed, so you won't be able to just grab files from anywhere in your user folder. Here's how to add that file to your playground to make it accessible:

  1. 在Finder中找到您的".playground"文件
  2. 右键单击并选择显示包装内容"
  3. 您应该看到"timeline.xctimeline","contents.xcplayground"和"section-1.swift"
  4. 新建一个名为资源"的文件夹
  5. 将"app.xml"复制到该新文件夹中

现在,您的文件将在沙箱中可用.您可以从捆绑包中检索它,并将其加载到NSXMLParser中,如下所示:

Now your file will be available inside the sandbox. You can retrieve it from the bundle and load it into the NSXMLParser like this:

let url: NSURL! = NSBundle.mainBundle().URLForResource("app", withExtension: "xml")
var xml = NSXMLParser(contentsOfURL: url)

这篇关于快速进行XML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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