我正在编写一个相当简单的R脚本,它将生成一些XML。 我正在使用XML包并执行以下操作:
doc <- newXMLDoc() root = newXMLNode("Root", namespaceDefinitions=c("xsi"="http://www.w3.org/2001/XMLSchema-instance"), doc=doc) newXMLNode("Foo", sample(g_IDS, 1), parent=root) #g_IDS is a vector with some random guids然后很多:
bar = newXMLNode("bar", parent = root)添加了更多节点,bar也获得了一些子节点。 有很多对newXMLNode的调用,我没有删除任何节点(至少不是故意的)。
最后,我将所有内容写入如下文件:
cat(saveXML(doc, file = "out.xml"))这有效,但我收到50条警告说:
在removeNodes.list(kids)中:removeNode目前仅适用于内部节点
我无法为我的生活找出原因。 有人知道吗?
I am writing a fairly simple R script that will generate some XML. I am using the XML package and doing something like this:
doc <- newXMLDoc() root = newXMLNode("Root", namespaceDefinitions=c("xsi"="http://www.w3.org/2001/XMLSchema-instance"), doc=doc) newXMLNode("Foo", sample(g_IDS, 1), parent=root) #g_IDS is a vector with some random guidsAnd then a lot of:
bar = newXMLNode("bar", parent = root)More nodes are added and bar gets some child nodes as well. There are a lot of calls to newXMLNode and I am not removing any nodes (at least not knowingly).
At the end I write everything to a file like this:
cat(saveXML(doc, file = "out.xml"))This works, but I get 50 warnings that say:
In removeNodes.list(kids) : removeNode only works on internal nodes at present
and I cannot for the life of me figure out why. Does anyone know?
最满意答案
最近遇到这种情况,修复来自正确下标列表的任何命名元素。 可能g_IDS维护其元素的名称,因此考虑获取返回的sample()第一个元素: newXMLNode("Foo", sample(g_IDS, 1)[[1]], parent=root)
展示:
g_IDs <- setNames(as.list(seq(1,50,1)), paste0("ID", seq(1,50,1))) # $ID1 # [1] 1 # $ID2 # [1] 2 # $ID3 # [1] 3 ... doc <- newXMLDoc() root = newXMLNode("Root", namespaceDefinitions=c("xsi"="http://www.w3.org/2001/XMLSchema-instance"), doc=doc) newXMLNode("Foo", sample(g_IDs, 1), parent=root) # <Foo>1</Foo> # Warning message: # In removeNodes.list(kids) : # removeNode only works on internal nodes at present doc <- newXMLDoc() root = newXMLNode("Root", namespaceDefinitions=c("xsi"="http://www.w3.org/2001/XMLSchema-instance"), doc=doc) newXMLNode("Foo", sample(g_IDs, 1)[[1]], parent=root) # <Foo>4</Foo>Recently running into this situation, the fix came with properly subscripting any named elements of list. Possibly g_IDS maintains names to its elements, so consider taking first element of the returned sample(): newXMLNode("Foo", sample(g_IDS, 1)[[1]], parent=root)
To demonstrate:
g_IDs <- setNames(as.list(seq(1,50,1)), paste0("ID", seq(1,50,1))) # $ID1 # [1] 1 # $ID2 # [1] 2 # $ID3 # [1] 3 ... doc <- newXMLDoc() root = newXMLNode("Root", namespaceDefinitions=c("xsi"="http://www.w3.org/2001/XMLSchema-instance"), doc=doc) newXMLNode("Foo", sample(g_IDs, 1), parent=root) # <Foo>1</Foo> # Warning message: # In removeNodes.list(kids) : # removeNode only works on internal nodes at present doc <- newXMLDoc() root = newXMLNode("Root", namespaceDefinitions=c("xsi"="http://www.w3.org/2001/XMLSchema-instance"), doc=doc) newXMLNode("Foo", sample(g_IDs, 1)[[1]], parent=root) # <Foo>4</Foo>更多推荐
发布评论