如何使用 R 将嵌套列表转换为 xml [英] How to convert nested list to xml Using R

查看:29
本文介绍了如何使用 R 将嵌套列表转换为 xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 kml 文件,想在特定位置添加节点,所以我写了这段代码 ..

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

这是我发现将 html 代码添加到 xml 代码中的特定位置的唯一方法,但是当我将顶部"转换为描述"时,数据的结构发生了变化并变得无用,所以有没有将 html 代码附加到 xml_data 而不将top"转换为列表的方法?

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 ?

我得到了一个将嵌套列表转换为 xml 的函数,但问题是用 html 编写的代码将转换为 xml 并且不再有用.

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)

这个功能由 Jeff Allen 在这个 link

this fuction written by Jeff Allen at this link

所以,请问有什么方法可以将此 html 代码附加到 xml 中,并且在将列表解析为 xml 时,html 代码仍然是 html 而不会转换为 xml 吗?

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 ?

这是我的 kml 文件

推荐答案

htmltools 包允许您构建嵌套节点,并且您可以使用 xmlParseString() 函数来获取您的节点:

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> 

这篇关于如何使用 R 将嵌套列表转换为 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆