I'm working with XML files from clinicaltrials.gov, which have a structure like this:
<clinical_study>
...
<brief_title>
...
<location>
<facility>
<name>
<address>
<city>
<state>
<zip>
<country>
</facility>
</location>
<location>
...
</location>
...
</clinical_study>
I'm gathering information from multiple XML files, so the number of locations in each file is unknown and could even be zero. I need to extract all the information about each location and save into an SQL table. I've had some success using functions from the XML package to extract information from single nodes, e.g.
library(XML)
nct_url <- "http://clinicaltrials.gov/ct2/show/NCT00112281?resultsxml=true"
xml_doc <- xmlParse(nct_url, useInternalNode=TRUE)
title_path <- "/clinical_study/brief_title"
title_text <- xpathSApply(xml_doc, title_path, xmlValue)
I'm experimenting with getNodeSet, and this gives me a set of the right length:
doc <- xmlParse("NCT00007501.xml")
locations <- getNodeSet(doc, "/clinical_study/location")
length(locations)
[1] 22
> class(locations)
[1] "XMLNodeSet"
but my attempts to extract information from this set have been mostly fruitless. Any suggestions?
This code will put a subset of nodes that correspond to
<location>
from a clinical trial into a data frame:In this case there are 221 locations. However, the code assumes sort of a flat structure and lumps subnodes together. For example, anything under
<facility>
gets concatenated into a single string. I can go into the subnodes and put them one by one into a dataframe.I don't understand why do you not use again
xpathSApply
, to retrieve locations as you already did for titles?!Here is an example