xml parsing - How to convert nested list to xml Using R -
i've kml file , wanted add nodes @ specific place it, wrote code ..
library(xml) kml.text <- readlines("c:/users/pc/downloads/googletraffic/maps/all maps.kml") xml_data <- xmltolist(kml.text) top = newxmlnode("description") table = newxmlnode("table ", attrs = c(width = 300, border = 1), parent = top) tbody <- newxmlnode("tbody",parent = tr) tr <- newxmlnode("tr",parent = table) th <- newxmlnode("th",attrs = c(scope = "col"),scope1 = max(all$traveltime),parent = tr) th <- newxmlnode("th",attrs = c(scope = "col"),scope1 = "md",parent = tr) th <- newxmlnode("th",attrs = c(scope = "col"),scope1 = "pm",parent = tr) tr <- newxmlnode("tr",parent = table) th <- newxmlnode("th",attrs = c(scope = "col"),scope1 = max(all$traveltime),parent = tr) th <- newxmlnode("th",attrs = c(scope = "col"),scope1 = "md",parent = tr) th <- newxmlnode("th",attrs = c(scope = "col"),scope1 = "pm",parent = tr) tr <- newxmlnode("tr",parent = table) th <- newxmlnode("th",attrs = c(scope = "col"),scope1 = max(all$traveltime),parent = tr) th <- newxmlnode("th",attrs = c(scope = "col"),scope1 = "md",parent = tr) th <- newxmlnode("th",attrs = c(scope = "col"),scope1 = "pm",parent = tr) th <- newxmlnode("img",attrs = c(src = url,width = "700",height= "777",alt=""),parent =top ) top description <- xmltolist(top) xml_data$document$folder$folder$folder$placemark$description <- description
that way found add html code specific position in xml code, when convert "top" "description" , structure of data got changed , become useless,so there way attached html code xml_data without converting "top" list ?
and got function convert nested list xml, problem code writen in html convert xml , not useful anymore.
root <- newxmlnode("root") listtoxml <- function(node, sublist){ for(i in 1:length(sublist)){ child <- newxmlnode(names(sublist)[i], parent=node); if (typeof(sublist[[i]]) == "list"){ listtoxml(child, sublist[[i]]) } else{ xmlvalue(child) <- sublist[[i]] } } } listtoxml(root,xml_data)
this fuction written jeff allen @ link
so, please there way attach html code xml , when parsing list xml, html code still html , not convert xml ?
here's kml file
the htmltools
package lets build out nested nodes , can use xmlparsestring()
function nodes:
library(htmltools) library(magrittr) tag("a", list(attr1="a1", attr2="a2", tag("b", list(tag("c", list(attr1="c1", "c content")), "b content")), "a content")) %>% tostring() %>% xmlparsestring() %>% str() ## classes 'xmlinternalelementnode', 'xmlinternalnode', 'xmlabstractnode' <externalptr>
Comments
Post a Comment