File: itunesSax.R

package info (click to toggle)
r-cran-xml 3.99-0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,688 kB
  • sloc: ansic: 6,659; xml: 2,890; asm: 486; sh: 12; makefile: 2
file content (60 lines) | stat: -rw-r--r-- 1,524 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
saxHandlers =
function()
{
  tracks = list()

  dictLevel = 0L
  key = NA
  value = character()

  track = list()
  
  text = function(val) {
          value <<- paste(value, val, sep = "")
        }

  startElement =
    function(name, attrs) {
       if(name %in% c("integer", "string", "date", "key"))
         value <<- character()

       if(name == "dict")
         dictLevel <<- dictLevel + 1L
    }


  convertValue =
    function(value, textType) {
      switch(textType,
             integer = as.numeric(value),
             string = value,
             date = as.POSIXct(strptime(value, "%Y-%m-%dT%H:%M:%S")),
             default = value)
     }
  
  endElement = function(name) {

    if(name %in% c("integer", "string", "date")) 
       track[[key]] <<- convertValue(value, name)
    else if(name == "key")
       key <<- value
    
    else if(name == "dict" && dictLevel == 3) {
      class(track) = "iTunesTrackInfo"
      tracks[[ length(tracks) + 1]] <<- track
      track <<- list()
      dictLevel <<- 2
    }
  }

  list(startElement = startElement, endElement = endElement, text = text, tracks = function() tracks)
}  

h = saxHandlers()
#xmlEventParse(path.expand(fileName), handlers = h)

# 5.9 seconds. But this is parsing and processing into tracks.
# system.time({dd = xmlEventParse(path.expand(fileName), handlers = h, addContext = FALSE)})

# 5.93 seconds on average (SD of .09)
# sax = replicate(10, system.time({dd = xmlEventParse(path.expand(fileName), handlers = h, addContext = FALSE)}))