1

I was looking at this example for parsing xml using swift http://ashishkakkad.com/2014/10/xml-parsing-in-swift-language-ios-8-nsxmlparser/

as you can see in the example, the xml file is located somewhere in the web

var url:String="http://api.androidhive.info/pizza/?format=xml"
var urlToSend: NSURL = NSURL(string: url)

I want to do the same, but instead I want to have the xml file locally as part of my single view project. Where in the project I should place the xml file?

How can I access it from the file?

2 Answers 2

3

Just drag the file anywhere in your XCode project. Then you just access it like

let xmlPath = NSBundle.mainBundle().pathForResource(fileName, ofType: "xml")
let xmlData = NSData(contentsOfFile: xmlPath)

Now you can parse your xmlData.I use AEXML for that purpose, but you can use any other libraries.

Sign up to request clarification or add additional context in comments.

1 Comment

@borna glad you're getting with it, mark as answer please!
0

Updated Code

let xmlPath = Bundle.main.path(forResource: "testfile", ofType: "xml") let xmlData = NSData(contentsOfFile: xmlPath!)

1 Comment

Better use Bundle.main.url than Bundle.main.path and better use Data than NSData.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.