[R] Modifying values into XML with R

Bastien.Ferland-Raymond at mrnf.gouv.qc.ca Bastien.Ferland-Raymond at mrnf.gouv.qc.ca
Thu Apr 26 14:04:51 CEST 2012


Dear R gurus,

I use R all the time at work, so one day a problem managing my personal arise data made me think: "Why not use R, it does everything!".

Anyway, my goal is to use R to manage my personal music library, and more precisely my playcounts.  I have two XML files, one from Winamp and the other one from Itunes.  Both have pretty much the same songs, but their playcounts are different. I want to import both of them in R, merge their playcounts and export it back to a XML file that I will be able to reload in Winamp (or Itunes).  So far, I managed to import in R both libraries, extract their playcounts and merge them.  But now I'm stuck at putting back this new playcount into the original XML.

Here is a reproducible example showing what I want to do:

######

### first download one of my xml from :
##  https://www.dropbox.com/s/qxteao3z8ypyfqh/petitXMLwinamp.xml


## load it in R and root it:
winamp<-xmlTreeParse("petitXMLwinamp.xml", useInternal = T)
racine <- xmlRoot(winamp)

racine        #  to view the library


###  I can extract one song (the first one for the example):
une.chanson <- xmlSApply(racine[[1]][["dict"]][[2]],xmlValue)


###  I can extract the playcount of this track with:
racine[[1]][["dict"]][[2]][which.max(une.chanson =="Play Count")+1]

### Now, I would simply want to change it from 2 to, lets say, 17:
racine[[1]][["dict"]][[2]][which.max(une.chanson =="Play Count")+1] <- 17

###  it doesn't work, I get the error:
Error in racine[[1]][["dict"]][[2]][which.max(une.chanson == "Play Count") +  :
  object of type 'externalptr' is not subsettable

###  If I try again digging further into the node I get the same error but with a different outcome:
racine[[1]][["dict"]][[2]][which.max(une.chanson =="Play Count")+1][[1]][[1]]<-17
#Error in racine[[1]][["dict"]][[2]][which.max(une.chanson == "Play Count") +  :
#  object of type 'externalptr' is not subsettable
racine[[1]][["dict"]][[2]][which.max(une.chanson =="Play Count")+1]
#$integer
#<integer>217</integer>

#attr(,"class")
#[1] "XMLInternalNodeList" "XMLNodeList"

###  It created an error, but appended 17 to the 2 to create 217...

######

Anybody here have an idea how to just change values of my XML document?  Also, the class of my playcount node is "XMLNodeList".  What is a "XMLNodeList"?  I can't find any reference of it in the XML package manual, so I don't know how to manage it and create it.  The solution may be into switching from XMLNodeList to XMLNode and back to XMLNodeList.  Anyway, those things are kind of complicated, I don't think I understand well yet the whole XML structure.


Thanks in advance for your help, don't hesitate to ask questions if you need precision.

Bastien
R version 2.14.1 (2011-12-22)
Platform: i386-pc-mingw32/i386 (32-bit)
on Windows



More information about the R-help mailing list