borna
borna

Reputation: 924

Parsing local xml file with iOS swift

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?

Upvotes: 1

Views: 3407

Answers (2)

U.Vishnu
U.Vishnu

Reputation: 1

Updated Code

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

Upvotes: 0

s1ddok
s1ddok

Reputation: 4654

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.

Upvotes: 3

Related Questions