3 Replies Latest reply on Mar 7, 2007 6:49 PM by ntsiii

    parsing iTunes generated XML

    Level 7
      Hi All-
      I'm trying to use an xml file exported from iTunes as the source of my
      datagrid but I'm having trouble getting it to pick up the data. A sample of
      the XML is below. I was trying to use a labelFunction but it isn't working.
      I'm sure it has something to do with the names being stored in <key></key>
      but I am not sure how to get around this. Any help would be appreciated or
      if this has been asked before some ideas of what I can search on would be
      great. I am not sure what exactly this sort of XML structure is called.
      Thanks,
      -Dan

      <dict>
      <key>Major Version</key><integer>1</integer>
      <key>Minor Version</key><integer>1</integer>
      <key>Application Version</key><string>7.0.2</string>
      <key>Features</key><integer>1</integer>
      <key>Show Content Ratings</key><true/>
      <key>Music
      Folder</key><string>file://localhost/D:/Music/iTunes/iTunes%20Music/</string>
      <key>Library Persistent ID</key><string>5ED56DCA1BEDBBBD</string>
      <key>Tracks</key>
      <dict>
      <key>12627</key>
      <dict>
      <key>Track ID</key><integer>12627</integer>
      <key>Name</key><string>Live Life</string>
      <key>Artist</key><string>ZOEgirl</string>
      <key>Album Artist</key><string>Zoe Girl</string>
      <key>Genre</key><string>Gospel</string>
      <key>Kind</key><string>MPEG audio file</string>
      <key>Size</key><integer>5843072</integer>
      <key>Total Time</key><integer>243435</integer>
      <key>Date Modified</key><date>2005-01-25T08:56:44Z</date>
      <key>Date Added</key><date>2007-02-02T07:05:19Z</date>
      <key>Bit Rate</key><integer>192</integer>
      <key>Sample Rate</key><integer>44100</integer>
      <key>Normalization</key><integer>7356</integer>
      <key>Persistent ID</key><string>7042A9A362F5B71B</string>
      <key>Track Type</key><string>File</string>
      <key>Location</key><string>file://localhost/D:/Music/ZOEgirl/ZOEgirl%20-%20Live%20Life.mp 3</string>
      <key>File Folder Count</key><integer>-1</integer>
      <key>Library Folder Count</key><integer>-1</integer>
      </dict>
      <key>12628</key>
      <dict>
      <key>Track ID</key><integer>12628</integer>
      <key>Name</key><string>Living For You</string>
      <key>Artist</key><string>ZOEgirl</string>
      <key>Genre</key><string>Gospel</string>
      <key>Kind</key><string>MPEG audio file</string>
      <key>Size</key><integer>5440558</integer>
      <key>Total Time</key><integer>226638</integer>
      <key>Date Modified</key><date>2005-01-31T20:24:18Z</date>
      <key>Date Added</key><date>2007-02-02T07:05:19Z</date>
      <key>Bit Rate</key><integer>192</integer>
      <key>Sample Rate</key><integer>44100</integer>
      <key>Normalization</key><integer>10485</integer>
      <key>Persistent ID</key><string>7042A9A362F5B71F</string> <key>Track
      Type</key><string>File</string>
      <key>Location</key><string>file://localhost/D:/Music/ZOEgirl/ZOEgirl%20-%20Living%20For%2 0You.mp3</string>
      <key>File Folder Count</key><integer>-1</integer>
      <key>Library Folder Count</key><integer>-1</integer>
      </dict>

        • 1. Re: parsing iTunes generated XML
          ntsiii Level 3
          So I guess you want the Track info in the DataGrid?

          I'll play with this for a few minutes and see what I can come up with.

          Hmm, that XML is not valid. Is it a good copy?

          tracy
          • 2. Re: parsing iTunes generated XML
            Level 7
            Thanks Tracy-
            I actually see that the last two </dict>'s got cut off so the real xml has
            </dict></dict> added at the end of what I copied before. If it is any
            easier - is there a way to get the xml easily imported into a mysql
            database? I could then accomplish the same thing. Thanks again for taking
            a look at it,
            -Dan


            "ntsiii" <tspratt@lariatinc.com> wrote in message
            news:esl732$992$1@forums.macromedia.com...
            > So I guess you want the Track info in the DataGrid?
            >
            > I'll play with this for a few minutes and see what I can come up with.
            >
            > Hmm, that XML is not valid. Is it a good copy?
            >
            > tracy

            • 3. Re: parsing iTunes generated XML
              ntsiii Level 3
              Don't know about the db side, but here is an example that works. Sorry about the formatting, I'll send it direct to you if you send me an email.
              Tracy
              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical"
              creationComplete="initApp()">
              <mx:Script><![CDATA[
              import mx.collections.XMLListCollection;

              [Bindable]private var _xlcTracks:XMLListCollection;
              private function initApp(): void
              {
              _xlcTracks = new XMLListCollection(iTunesData.dict.dict); //get to the track "dict" nodes
              }//

              private function lfnTracks(oItem:Object, dgc:DataGridColumn):String
              {
              var xmlItem:XML = XML(oItem);
              var sHeaderText:String = dgc.headerText; //so we can check which column we are working with
              var xl:XMLList = xmlItem.children(); //list of all the child nodes of Track "dict"
              var xmlKey:XML = xmlItem.key.(text()==sHeaderText)[0];//find the <key> node that matches the header text
              var iChildIndex:int = xmlKey.childIndex(); //get its index
              var xmlValue:XML = xl[iChildIndex+1] //find the next node
              var sLabel:String = xmlValue.text(); //ger and return the text() value
              return sLabel;
              }//lfnTracks

              ]]></mx:Script>

              <mx:DataGrid id="dgTracks" dataProvider="{_xlcTracks}"
              editable="true" >
              <mx:columns>
              <mx:Array>
              <mx:DataGridColumn headerText="Name" labelFunction="lfnTracks" />
              <mx:DataGridColumn headerText="Artist" labelFunction="lfnTracks" />
              <mx:DataGridColumn headerText="Genre" labelFunction="lfnTracks" />
              </mx:Array>
              </mx:columns>
              </mx:DataGrid>

              <mx:XML id="iTunesData" xmlns="">
              <dict>
              <key>Major Version</key>
              <integer>1</integer>
              <key>Minor Version</key>
              <integer>1</integer>
              <key>Application Version</key>
              <string>7.0.2</string>
              <key>Features</key>
              <integer>1</integer>
              <key>Show Content Ratings</key>
              <true/>
              <key>Music Folder</key>
              <string>file://localhost/D:/Music/iTunes/iTunes%20Music/</string>
              <key>Library Persistent ID</key>
              <string>5ED56DCA1BEDBBBD</string>
              <key>Tracks</key>
              <dict>
              <key>12627</key>
              <dict>
              <key>Track ID</key>
              <integer>12627</integer>
              <key>Name</key>
              <string>Live Life</string>
              <key>Artist</key>
              <string>ZOEgirl</string>
              <key>Album Artist</key>
              <string>Zoe Girl</string>
              <key>Genre</key>
              <string>Gospel</string>
              </dict>
              <key>12628</key>
              <dict>
              <key>Track ID</key>
              <integer>12628</integer>
              <key>Name</key>
              <string>Living For You</string>
              <key>Artist</key>
              <string>ZOEgirl</string>
              <key>Genre</key>
              <string>Gospel</string>
              </dict>
              </dict>
              </dict>
              </mx:XML>
              </mx:Application>