如何从 R 对象创建 xml,例如,是否有“listToXml"函数? [英] How to create xml from R objects, e.g., is there a 'listToXml' function?

查看:20
本文介绍了如何从 R 对象创建 xml,例如,是否有“listToXml"函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R 的 XML 包有一个 xmlToList 函数,但是没有反向,是有一个 R 函数可以将列表转换为 XML 对象?

R's XML package has an xmlToList function, but does not have the reverse, is there a function for R that will convert a list to an XML object?

我想要类似的东西

listToXML(list('a'))

返回

<a></a>

但我能找到的最接近的是

but the closest I can find is

library(XML)
xmlNode(list('a'))

哪个返回

</a>

关于这个问题的帮助,以及理解 R 对象到 XML 的转换通常受到赞赏(XML 包似乎更侧重于使用 R 来读取 XML,对创建 XML 的支持较少).

help on this question, and understanding the conversion of R objects to XML in general appreciated (the XML package appears more focused on the use of R to read XML, with less support for creating XML).

更新... 我无法弄清楚这一点的一个原因是我没有意识到 <node/> 中的尾随 '/' 表示一个空节点,相当于

Update... One reason that I could not figure this out is because I did not realize that the trailing '/' in <node/> indicates an empty node, equivalent to <node></node>

推荐答案

newXMLNode 函数可以完成您的需求,即编写 XML 输出.有关更多详细信息,请参阅 ?newXMLNode 中的详细帮助和示例.这是一个简短的摘录:

The function newXMLNode does what you need, i.e., write XML output. See the detailed help and examples in ?newXMLNode for more details. Here is a short extract:

library(XML)    
top = newXMLNode("a")
newXMLNode("b", attrs=c(x=1, y='abc'), parent=top)
newXMLNode("c", "With some text", parent=top)
top

结果:

<a>
  <b x="1" y="abc"/>
  <c>With some text</c>
</a> 

这篇关于如何从 R 对象创建 xml,例如,是否有“listToXml"函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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