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

1
  • I wrote the answer here) , Please check it . Commented Sep 24, 2016 at 0:45

1 Answer 1

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> 
Sign up to request clarification or add additional context in comments.

7 Comments

thanks alot , but i made a node before, the problem now is to convert the nested list to xml
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.
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
you need to rebuild your list with htmltools tags or modify the xmlToList answer to handle the nesting.
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)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.