I am trying to convert xml content to DataFrame. The xml is as follows:
<group>
<data>
<metadata>
<meta content="6 cyl" name="engine"/>
<meta content="55" name="mpg"/>
<meta content="2700" name="weight"/>
</metadata>
</data>
<data>
<metadata>
<meta content="3 cyl" name="engine"/>
<meta content="65" name="mpg"/>
<meta content="2420" name="weight"/>
</metadata>
</data>
</group>
and I want the DataFrame as follows:
engine mpg weight
6 cyl 55 2700
3 cyl 65 2400
I tried this:
data <- read_xml("myFile.xml")
meta <- data %>% xml_find_all("//meta")
df <- data.frame(name = sapply(meta %>% xml_attr("name"), as.character),
content = sapply(meta %>% xml_attr("content"), as.character))
But it produces this DataFrame:
name content
engine 6 cyl
mpg 55
weight 2700
engine 3 cyl
mpg 65
weight 2420
then...
df <- df %>% spread(unique(name), content)
Produces an error:
Error: Duplicate identifiers for rows....
Is my approach correct, or there is another way to achieve this?