I've kml file and wanted to add nodes at specific place into it, so i wrote this 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 was the only way i found to add the html code to a specific position in the xml code, but when i convert "top" to "description" , the structure of data got changed and become useless,so is there any way to attached the html code to the xml_data without converting "top" to a list ?
and i got a function that convert nested list to xml, but the problem is that the code which writen in html will convert to xml and will 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 by Jeff Allen at this link
so, Please is there any way to attach this html code to the xml and when parsing the list to xml, the html code still html and not convert to xml ?
here's my kml file