-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoecd_code_list.R
More file actions
65 lines (45 loc) · 1.54 KB
/
oecd_code_list.R
File metadata and controls
65 lines (45 loc) · 1.54 KB
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
61
62
63
64
65
library('XML')
m_url <- 'https://webfs.oecd.org/crs-iati-xml/Lookup/DAC-CRS-CODES.xml'
con <- url(m_url)
codes <- readLines(con)
close(con)
codes <- as.character(codes)
#Parse XML to list
xmldata <- xmlToList(codes)
#There is a listof code lists, extract each in a loop or write a function and pass to apply
#Working with one just to get content
xmldata_codelist <- as.list(xmldata[[1]])
#Each code list has a list of items, get the items
code_list_items <- as.list(xmldata_codelist$`codelist-items`)
#Loop through each code list item and get the data details
getcodelistitems <- function(codelistitem){
item <- c(
codelistitem$code,
codelistitem$name$narrative,
codelistitem$category,
#codelistitem$description$reference,
codelistitem$description$narrative
)
}
for(i in 1:length(xmldata)){
xmldata_codelist <- as.list(xmldata[[i]])
name <- xmldata_codelist$metadata$name$narrative
data <- matrix(ncol = 4,nrow = 0)
code_list_items <- as.list(xmldata_codelist$`codelist-items`)
for(k in 1:length(code_list_items)){
item <- getcodelistitems(code_list_items[[k]])
data <- mapply(c, data,item)
# rbind(data,item)
}
assign(name,data.frame(data))
}
xmldata$metadata$name[[1]]
codelist <- as.list(xmldata$`codelist-items`)
codeListNames <- function(xml){
#Iterate over this list and pick all content
xmldata <- as.list(xmldata[[1]])
codelist <- as.list(xmldata$`codelist-items`)
narrative <- xmldata[[1]]$metadata$narrative
}
paste0('value') <- 'test'
print(sapply(xmldata,codeListNames))