[英]formatting saveXML output to retain tree structure in R
I have a very long xml file that I am generating in R, and when I print the final xml to the console, it keeps the visual tree structure that makes the output easy for a human to read. 我在R中生成了一个非常长的xml文件,当我将最终的xml打印到控制台时,它保持了可视化的树状结构,使输出易于人们阅读。 When I write it to a file using saveXML though, it prints out one HUGE single line. 当我使用saveXML将其写入文件时,它会打印出一行巨大的单行。 Does anyone know how to get the saveXML function to print this and maintain the multi-line tree structure? 有谁知道如何获取saveXML函数来打印此内容并维护多行树结构? I have looked everywhere and cannot find a solution. 我到处都是,找不到解决方案。
A small toy example (with code taken from https://stat.ethz.ch/pipermail/r-help/2008-September/175364.html ) : 一个小玩具示例(代码取自https://stat.ethz.ch/pipermail/r-help/2008-September/175364.html ):
data <- read.csv(textConnection('"date","UYG.Open","UYG.High","UYG.Low","UYG.Close","UYG.Volume","UYG.Adjusted"
"2007-02-01",71.32,71.34,71.32,71.34,200,69.23
"2007-02-02",72.2,72.2,72.2,72.2,200,70.06
"2007-02-05",71.76,71.76,71.76,71.76,5100,69.63
"2007-02-06",72.85,72.85,72.85,72.85,3800,70.69
"2007-02-07",72.85,72.85,72.85,72.85,0,70.69'),
as.is=TRUE)
library(XML)
xml <- xmlTree()
xml$addTag("document", close=FALSE)
for (i in 1:nrow(data)) {
xml$addTag("row", close=FALSE)
for (j in names(data)) {
xml$addTag(j, data[i, j])
}
xml$closeTag()
}
xml$closeTag()
# view the result
cat(saveXML(xml))
saveXML(xml, file="~/Desktop/xml_format_test.xml", compression = 0, indent=TRUE, prefix = '<?xml version="1.0"?>\n', doctype = NULL)
When I run the ' cat(saveXML(xml))
command, I get the following structure: 当我运行' cat(saveXML(xml))
命令时,我得到以下结构:
<?xml version="1.0"?>
<document>
<row>
<date> 2007-02-01</date>
<UYG.Open>71.32</UYG.Open>
<UYG.High>71.34</UYG.High>
<UYG.Low>71.32</UYG.Low>
<UYG.Close>71.34</UYG.Close>
<UYG.Volume>200</UYG.Volume>
<UYG.Adjusted>69.23</UYG.Adjusted>
</row>
<row>
<date> 2007-02-02</date>
<UYG.Open>72.2</UYG.Open>
<UYG.High>72.2</UYG.High>
<UYG.Low>72.2</UYG.Low>
<UYG.Close>72.2</UYG.Close>
<UYG.Volume>200</UYG.Volume>
<UYG.Adjusted>70.06</UYG.Adjusted>
</row>
<row>
<date> 2007-02-05</date>
<UYG.Open>71.76</UYG.Open>
<UYG.High>71.76</UYG.High>
<UYG.Low>71.76</UYG.Low>
<UYG.Close>71.76</UYG.Close>
<UYG.Volume>5100</UYG.Volume>
<UYG.Adjusted>69.63</UYG.Adjusted>
</row>
<row>
<date> 2007-02-06</date>
<UYG.Open>72.85</UYG.Open>
<UYG.High>72.85</UYG.High>
<UYG.Low>72.85</UYG.Low>
<UYG.Close>72.85</UYG.Close>
<UYG.Volume>3800</UYG.Volume>
<UYG.Adjusted>70.69</UYG.Adjusted>
</row>
<row>
<date> 2007-02-07</date>
<UYG.Open>72.85</UYG.Open>
<UYG.High>72.85</UYG.High>
<UYG.Low>72.85</UYG.Low>
<UYG.Close>72.85</UYG.Close>
<UYG.Volume>0</UYG.Volume>
<UYG.Adjusted>70.69</UYG.Adjusted>
</row>
</document>
But when I print this using saveXML(xml, file="~/Desktop/xml_format_test.xml", compression = 0, indent=TRUE, prefix = '<?xml version="1.0"?>\\n', doctype = NULL)
, I get the following structure: 但是当我使用saveXML(xml, file="~/Desktop/xml_format_test.xml", compression = 0, indent=TRUE, prefix = '<?xml version="1.0"?>\\n', doctype = NULL)
打印此saveXML(xml, file="~/Desktop/xml_format_test.xml", compression = 0, indent=TRUE, prefix = '<?xml version="1.0"?>\\n', doctype = NULL)
,我得到以下结构:
<?xml version="1.0"?>
<document> <row> <date> 2007-02-01</date> <UYG.Open>71.32</UYG.Open> <UYG.High>71.34</UYG.High> <UYG.Low>71.32</UYG.Low> <UYG.Close>71.34</UYG.Close> <UYG.Volume>200</UYG.Volume> <UYG.Adjusted>69.23</UYG.Adjusted> </row> <row> <date> 2007-02-02</date> <UYG.Open>72.2</UYG.Open> <UYG.High>72.2</UYG.High> <UYG.Low>72.2</UYG.Low> <UYG.Close>72.2</UYG.Close> <UYG.Volume>200</UYG.Volume> <UYG.Adjusted>70.06</UYG.Adjusted> </row> <row> <date> 2007-02-05</date> <UYG.Open>71.76</UYG.Open> <UYG.High>71.76</UYG.High> <UYG.Low>71.76</UYG.Low> <UYG.Close>71.76</UYG.Close> <UYG.Volume>5100</UYG.Volume> <UYG.Adjusted>69.63</UYG.Adjusted> </row> <row> <date> 2007-02-06</date> <UYG.Open>72.85</UYG.Open> <UYG.High>72.85</UYG.High> <UYG.Low>72.85</UYG.Low> <UYG.Close>72.85</UYG.Close> <UYG.Volume>3800</UYG.Volume> <UYG.Adjusted>70.69</UYG.Adjusted> </row> <row> <date> 2007-02-07</date> <UYG.Open>72.85</UYG.Open> <UYG.High>72.85</UYG.High> <UYG.Low>72.85</UYG.Low> <UYG.Close>72.85</UYG.Close> <UYG.Volume>0</UYG.Volume> <UYG.Adjusted>70.69</UYG.Adjusted> </row> </document>
I really need the output format in the multi-line tree format for ease of visualization and to help new users understand the output. 为了简化可视化并帮助新用户理解输出,我确实需要多行树格式的输出格式。 Any help is greatly appreciated as I have tried to find the solution for a few hours and cannot. 非常感谢您的帮助,因为我已经尝试了几个小时而无法找到解决方案。
You could just use the file
argument to cat()
: 您可以只使用cat()
的file
参数:
cat(saveXML(xml), file="~/Desktop/xml_format_test.xml")
Looks like this bug was spotted some years ago but still hasn't been fixed. 看起来该错误是几年前发现的,但尚未修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.