How to convert nested list to xml Using R

≯℡__Kan透↙ 提交于 2019-12-12 01:41:50

问题


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:


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> 


来源:https://stackoverflow.com/questions/39642725/how-to-convert-nested-list-to-xml-using-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!