0

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

Community
  • 1
  • 1
  • I wrote the answer [here](http://stackoverflow.com/questions/39623094/adding-a-tag-in-kml-file-using-r/39671366#39671366)) , Please check it . – Omar Abd El-Naser Sep 24 '16 at 00:45

1 Answers1

3

The htmltools package lets you build out nested nodes and you can use the xmlParseString() function to get your 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> 
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • thanks alot , but i made a node before, the problem now is to convert the nested list to xml – Omar Abd El-Naser Sep 22 '16 at 17:49
  • _that's what this does_. Remove the `xmlParseString()`. The whole thing makes a list turns it into XML and parses it into a node. You really need to try out things. – hrbrmstr Sep 22 '16 at 17:51
  • i'm spending hours trying codes to finish this work, i tried your code and tried it again on my data , here's what i tried x <- tag("a", xml_data) %>% toString() %>% xmlParseString() here's what i got Warning messages: 1: In if (!is.na(attribValue)) { : the condition has length > 1 and only the first element will be used 2: In charToRaw(enc2utf8(text)) : argument should be a character vector of length 1 all but the first element will be ignored – Omar Abd El-Naser Sep 22 '16 at 17:58
  • you need to rebuild your list with `htmltools` tags or modify the `xmlToList` answer to handle the nesting. – hrbrmstr Sep 22 '16 at 18:29
  • 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) – Omar Abd El-Naser Sep 22 '16 at 20:08
  • this function is fairly good for converting my xml_data to a xml, but the problem now is that the html code which was "top" in my code above turns to xml and it become useless, is there any way to change all the xml_data except the html code ? i swear i'm spending 2days 24/7 working on this problem .... btw i'm not a programmer, so there are so manythings i don't know – Omar Abd El-Naser Sep 22 '16 at 20:11
  • i added node to xml file but i can't write it out, please if u can help, check this link http://stackoverflow.com/questions/39666820/inserting-a-node-to-a-certain-node-in-xml-file-using-r?noredirect=1#comment66637540_39666820 – Omar Abd El-Naser Sep 23 '16 at 21:31