情况:将“软件”复制到R并返回到“软件”。 “软件”的唯一接口是xml。要列出并返回XML的XML
在R中,我需要对文件进行一些更改,以便将其转换为列表并进行一些更改。
library(XML)
myFile = xmlParse("myXML")
xml_data <- xmlToList(myFile)
xml_data$timetable$train$.attrs[6] = "HelloNewWorld"
现在我需要将此列表“xml_data”转换回xml。
我发现了一些功能,像这样:
function(item, tag) {
# just a textnode, or empty node with attributes
if(typeof(item) != 'list') {
if (length(item) > 1) {
xml <- xmlNode(tag)
for (name in names(item)) {
xmlAttrs(xml)[[name]] <- item[[name]]
}
return(xml)
} else {
return(xmlNode(tag, item))
}
}
# create the node
if (identical(names(item), c("text", ".attrs"))) {
# special case a node with text and attributes
xml <- xmlNode(tag, item[['text']])
} else {
# node with child nodes
xml <- xmlNode(tag)
for(i in 1:length(item)) {
if (names(item)[i] != ".attrs") {
xml <- append.xmlNode(xml, listToXml(item[[i]], names(item)[i]))
}
}
}
# add attributes to node
attrs <- item[['.attrs']]
for (name in names(attrs)) {
xmlAttrs(xml)[[name]] <- attrs[[name]]
}
return(xml)
}
但是,这并不工作...
任何帮助或暗示的赞赏!
谢谢!
在链接的图片中,您可以看到当前的xml文件。用黄色突出显示我需要更改的值。
链接:
https://i.stack.imgur.com/remzj.png
它可能有助于看到XML,当前和预期的结果。 – Parfait