I simply cannot get my head around how to make the childelements in a element as rows and the attributes in them as columns in a R dataframe for the XML structure seen below.
I know how to do it when the elements are in the root. Like:
data <- getURL(url) %>%
xmlTreeParse(., useInternal = TRUE) %>%
xmlRoot()
doc <- XML::xmlToList(data)
nodes <- grep("SubChild2_1", names(data))
out <- data.frame(do.call(rbind, lapply(nodes, function(x) doc[[x]])), stringsAsFactors = FALSE)
For this:
<Child1 filename="stuff" datetimecreated="date stuff" datetimeimported="stuff" stuffi="s" >
<SubChild1_1 attri1="attri1v" attri2="attri2v" />
<SubChild2_1 IMPODATE="2019-02-22" IMPOVALUE="1" IMPOVALUE2="123.35" IMPOVALUE3="12.85" />
<SubChild2_1 IMPODATE="2019-02-22" IMPOVALUE="2" IMPOVALUE2="654.35" IMPOVALUE3="23.85" />
<SubChild2_1 IMPODATE="2019-02-22" IMPOVALUE="3" IMPOVALUE2="789.35" IMPOVALUE3="34.85" />
</Child1>
But I struggle to find out how to look one step into the xmltree like for this one:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<response xmlns="http://www.xx.com/abc/def" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xx.com/abc/def http://www.xx.com/abc/def.xsd" status="1" timestamp="2019-03-06T20:24:14">
<Child1 filename="stuff" datetimecreated="date stuff" datetimeimported="stuff" stuffi="s" >
<SubChild1_1 attri1="attri1v" attri2="attri2v" />
<SubChild2_1 IMPODATE="2019-02-22" IMPOVALUE="1" IMPOVALUE2="123.35" IMPOVALUE3="12.85" />
<SubChild2_1 IMPODATE="2019-02-22" IMPOVALUE="2" IMPOVALUE2="654.35" IMPOVALUE3="23.85" />
<SubChild2_1 IMPODATE="2019-02-22" IMPOVALUE="3" IMPOVALUE2="789.35" IMPOVALUE3="34.85" />
</Child1>
</response>
Could anybody help me with that rather small annoying problem.