Tuesday, March 3, 2015

R30. Reading and parsing XML document in R

We will use a test xml file, from python documentation country_data.xml.


The package XML is used to parse the document.


The name of the country, the year, as well as the neighbors are returned for all 3 countries in document.

# ex30.R
require('XML')
doc <- xmlParse('ex30data.xml')
r <- xmlRoot(doc)
country <- xmlChildren(r)
for (i in 1:length(country)) {
  cat('\n',xmlAttrs(country[[i]]),'\n', sep="")
  cat("year : ")
  cyear <- country[[i]][['year']][[1]]
  print(cyear)
  cneigh <- country[[i]]["neighbor"]
  for (i in 1:length(cneigh)) {
    cat("neighbor",i,":")
    print(cneigh[[i]])
  }
}

# Liechtenstein
# year : 2008 
# neighbor 1 :<neighbor name="Austria" direction="E"/> 
# neighbor 2 :<neighbor name="Switzerland" direction="W"/> 
#   
# Singapore
# year : 2011 
# neighbor 1 :<neighbor name="Malaysia" direction="N"/> 
#   
# Panama
# year : 2011 
# neighbor 1 :<neighbor name="Costa Rica" direction="W"/> 
# neighbor 2 :<neighbor name="Colombia" direction="E"/> 

No comments:

Post a Comment