Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

macos - XML parsing in swift

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. Find your ".playground" file in the Finder
  2. Right click and choose "Show Package Contents"
  3. You should see "timeline.xctimeline", "contents.xcplayground", and "section-1.swift"
  4. Make a new folder called "Resources"
  5. Copy "app.xml" into that new folder

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...