I have a xml file with the following structure:
<?xml version="1.0" encoding="ISO-8859-1"?>
<LIST_ORDER><file><order>
<param name="Col1" value="A"/>
<param name="Col2" value="B"/>
<param name="Col3" value="C"/>
</order>
</file></LIST_ORDER>
I need to import it into R and transform in a dataframe. The desired output is as follows:
Col1 | A
Col2 | B
Col3 | C
Is there a function or method to accomplish that ?
Appreciate any help
CodePudding user response:
You can use the xml2
package. Read in the xml with read_xml
, identify the "param" nodes using xml_find_all
, then you can harvest the attribute values with xml_attr
library(xml2)
params <- read_xml(my_xml) |> xml_find_all(xpath = "//param")
data.frame(name = xml_attr(params, "name"), value = xml_attr(params, "value"))
#> name value
#> 1 Col1 A
#> 2 Col2 B
#> 3 Col3 C
Note that my_xml
can either be a path to your xml file, or the xml as a string. In this example I used:
my_xml <- '<?xml version="1.0" encoding="ISO-8859-1"?>
<LIST_ORDER>
<file>
<order>
<param name="Col1" value="A"/>
<param name="Col2" value="B"/>
<param name="Col3" value="C"/>
</order>
</file>
</LIST_ORDER>'
Created on 2022-08-26 with reprex v2.0.2