Reputation: 1
I have data like:
<location country="SWITZERLAND" city="" state="" xsi:nil="true"/>
<location country="URUGUAY" city="" state="" xsi:nil="true"/>
Above locations tag is of dynamic size in the XML, I have shown 2 countries only, but it could be 1 or 5 also. Now I want to extract the values like SWITZERLAND
, URUGUAY
, and store them in a single cell of the dataframe.
Upvotes: 0
Views: 58
Reputation: 11955
You can use xpathSApply
to fetch node's attributes
library(XML)
doc <- xmlParseDoc("test.xml")
df <- as.data.frame(t(xpathSApply(doc, "//location_tag/location", xmlAttrs)))
which gives
> df
country city state
1 SWITZERLAND
2 URUGUAY
Sample data: Note that I have slightly modified your sample data as you have not provided complete XML.
test.xml
contains
<location_tag>
<location country="SWITZERLAND" city="" state=""/>
<location country="URUGUAY" city="" state=""/>
</location_tag>
Upvotes: 1