Skip to content
Prev 181011 / 398502 Next

Example for parsing XML file?

Hi Brigid.

Here are a few commands that should do what you want:

bri = xmlParse("myDataFile.xml")

tmp =  t(xmlSApply(xmlRoot(bri), xmlAttrs))[, -1]
dd = as.data.frame(tmp, stringsAsFactors = FALSE,
                     row.names = 1:nrow(tmp))

And then you can convert the columns to whatever types you want
using regular R commands.

The basic idea is that for each of the child nodes of C,
i.e. the <T>'s, we want the character vector of attributes
which we can get with xmlAttrs().

Then we stack them together into a matrix, drop the "N"
and then convert the result to a data frame, avoiding
duplicate row names which are all "T".

(BTW, make certain the '-' on the second line is not in the XML content.
  I assume that came from bringing the text into mail.)

HTH
   D.
Brigid Mooney wrote: