Parsing XML data with Cocoa
I'm working on an iTunes menu bar controller and I want to get the names of all the songs in the user's library. I have found that there is an XML file called iTunes Library.xml.
It holds all the data for iTunes (name, artist, album etc) I want to be able to access th开发者_如何学Cat data to use in my controller.
I really have never used XML for anything so go easy…
I am also a noob at objective-C so go REALLY easy…
There are two things you are going to want to learn about to scan xml info. One is called xpath
and the other is called xquery
. On the surface they are both basically ways of creating a regular expression to grab different parts of xml data. Googling xpath should bring up many examples tor create any type of expression you can imagine. NSXMLParser
is a Cocoa class that can create a parser object and has the method nodesForXPath
which will take your xpath
query gather the results and store them in an array.
Apple's dev docs here
I'm pretty sure the iTunes XML library is an XML PLIST dictionary. You could save some headaches by reading up on NSDictionary and using the +dictionaryWithCintentsOf...: class method, then just navigating the structure by its keys.
If you open it with the Property List Editor app that comes with Xcode Tools, you can explore its structure visually, which will give you an idea how to navigate the dicts and arrays programmatically.
精彩评论