racketituneshtdp

Fixing "read-itunes-as-list: expects a file with XML document as first argument, given "itunes.xml"" for HTDP exercise 200


I'm doing the HtDP Exercises, and I'm having trouble on Exercise 200. I did what the book told me, but I kept on getting the same error. No matter what I tried, I always got this:

read-itunes-as-list: expects a file with XML document as first argument, given "itunes.xml"

I've tried changing the name of itunes.xml, and some other things, but they all seemed to fail. Does someone know how to solve this? Here's my code (so far):

; An LTracks is one of:
; – '()
; – (cons Track LTracks)
; Example:
;   (cons (make-track "Wild Child" "Enya" "A Day Without" 227996 2 (make-date 2002 7 17 3 55 14) 20 (make-date 2011 5 17 17 35 13))
;         '())

;(define-struct track
;  [name artist album time track# added play# played])
; A Track is a structure:
;   (make-track String String String N N Date N Date)
; interpretation An instance records in order: the track's 
; title, its producing artist, to which album it belongs, 
; its playing time in milliseconds, its position within the 
; album, the date it was added, how often it has been 
; played, and the date when it was last played
; Example:
;   (make-track "Wild Child" "Enya" "A Day Without" 227996 2 (make-date 2002 7 17 3 55 14) 20 (make-date 2011 5 17 17 35 13))

;(define-struct date [year month day hour minute second])
; A Date is a structure:
;   (make-date N N N N N N)
; interpretation An instance records six pieces of information:
; the date's year, month (between 1 and 12 inclusive), 
; day (between 1 and 31), hour (between 0 
; and 23), minute (between 0 and 59), and 
; second (also between 0 and 59).
; Example:
;   (make-date 2019 7 9 20 35 54)

; modify the following to use your chosen name
(define ITUNES-LOCATION "itunes.xml")

; LTracks
(define itunes-tracks
  (read-itunes-as-tracks ITUNES-LOCATION))

And itunes.xml:

<dict>
    <key>Track ID</key><integer>442</integer>
    <key>Name</key><string>Wild Child</string>
    <key>Artist</key><string>Enya</string>
    <key>Album</key><string>A Day Without</string>
    <key>Genre</key><string>New Age</string>
    <key>Kind</key><string>MPEG audio file</string>
    <key>Size</key><integer>4562044</integer>
    <key>Total Time</key><integer>227996</integer>
    <key>Track Number</key><integer>2</integer>
    <key>Track Count</key><integer>11</integer>
    <key>Year</key><integer>2000</integer>
    <key>Date Added</key><date>2002-7-17T3:55:14</date>
    <key>Play Count</key><integer>20</integer>
    <key>Play Date</key><integer>3388484113</integer>
    <key>Play Date UTC</key><date>2011-5-17T17:35:13
</dict>

Solution

  • As I mentioned in the comment, your itunes.xml is malformed. For example,

    • On the line before the last line, you have <date> but there's no corresponding ending tag </date>.
    • Date format is wrong. E.g., 2002-7-17T3:55:14 should be changed to 2002-07-17T03:55:14.

    If you download itunes.xml from https://gist.github.com/sorawee/e92eada7d3081b308c5e9fefbcb3def3 and use it instead, it should work fine.

    Where and how did you obtain your original itunes.xml?