[R] Parsing XML File

Lorenzo Isella lorenzo.isella at gmail.com
Sun Oct 11 21:10:14 CEST 2015


Dear All,
I am struggling with the parsing of the xml file you can find at

https://www.dropbox.com/s/i4ld5qa26hwrhj7/account.xml?dl=0

Essentially, I would like to be able to convert it to a data.frame to
manipulate it in R and detect all the attributes of an account for
which  unrealizedPNL goes above a threshold.
I stored that file as account.xml and looking here and there on the
web I put together the following script


#####################################################################
library(XML)

xmlfile=xmlParse("account.xml")

class(xmlfile) #"XMLInternalDocument" "XMLAbstractDocument"
xmltop = xmlRoot(xmlfile) #gives content of root
class(xmltop)#"XMLInternalElementNode" "XMLInternalNode"
"XMLAbstractNode"
xmlName(xmltop) #give name of node, PubmedArticleSet
xmlSize(xmltop) #how many children in node, 19
xmlName(xmltop[[1]]) #name of root's children

# have a look at the content of the first child entry
xmltop[[1]]
# have a look at the content of the 2nd child entry
xmltop[[2]]
#Root Node's children
number <- xmlSize(xmltop[[1]]) #number of nodes in each child
name <- xmlSApply(xmltop[[1]], xmlName) #name(s)
attribute <- xmlSApply(xmltop[[1]], xmlAttrs) #attribute(s)
size <- xmlSApply(xmltop[[1]], xmlSize) #size


values <- xmlSApply(xmltop, function(x) xmlSApply(x, xmlValue))
#####################################################################

which is leading me nowhere.
Any suggestion is appreciated.
Cheers

Lorenzo



More information about the R-help mailing list