How to convert xml data to data frame in R -
How to convert xml data to data frame in R -
hello guys, need load xml file info frame in r. xml format shown below. how acheive same?
<?xml version="1.0" encoding="utf-8"?><posts> <row id="1" posttypeid="1" acceptedanswerid="17" creationdate="2010-07-26t19:14:18.907" score="6"/></posts>
i tried below code....it not give desired output. expecting tabular output column names , values listed below.
library(xml) xml.url ="test.xml" xmlfile = xmltreeparse(xml.url) class(xmlfile) xmltop=xmlroot(xmlfile) print(xmltop)[1:2] plantcat <- xmlsapply(xmltop, function(x) xmlsapply(x, xmlvalue)) plantcat_df <- data.frame(t(plantcat))
xml.text <- '<?xml version="1.0" encoding="utf-8"?> <posts> <row id="1" posttypeid="1" acceptedanswerid="17" creationdate="2010-07-26t19:14:18.907" score="6"/> <row id="2" posttypeid="1" acceptedanswerid="17" creationdate="2010-07-26t19:14:18.907" score="6"/> <row id="3" posttypeid="1" acceptedanswerid="17" creationdate="2010-07-26t19:14:18.907" score="6"/> <row id="4" posttypeid="1" acceptedanswerid="17" creationdate="2010-07-26t19:14:18.907" score="6"/> </posts>' library(xml) xml <- xmlparse(xml.text) result <- as.data.frame(t(xmlsapply(xml["/posts/row"],xmlattrs)), stringsasfactors=false) # id posttypeid acceptedanswerid creationdate score # 1 1 1 17 2010-07-26t19:14:18.907 6 # 2 2 1 17 2010-07-26t19:14:18.907 6 # 3 3 1 17 2010-07-26t19:14:18.907 6 # 4 4 1 17 2010-07-26t19:14:18.907 6
this bit trickier usual because info in attributes, not nodes (the nodes empty), can't utilize xlmtodataframe(...)
unfortunately.
all info above still character, still need convert columns whatever class appropriate.
xml r xml-parsing
Comments
Post a Comment