Rexml - 带有内联文本和缩进的子标签的漂亮打印
我正在使用 REXML 构建 xml 文档,并希望以特定方式输出到文本。该文档是 CuePoint 标签的列表,我用 Element.new 和 add_element 生成的标签都被合并成一行,如下所示:(stackoverflow 在这里将它们分成两行,但想象一下以下内容都在一行):
当我将它们保存到文件中时,我希望它们看起来像这样
<CuePoint>
<Time>15359</Time>
<Type>event</Type>
<Name>inst_50</Name>
</CuePoint>
<CuePoint>
<Time>16359</Time>
<Type>event</Type>
<Name>inst_50</Name>
</CuePoint>
:尝试向 .write 函数传递值 2,以缩进它们:这会产生以下结果:
xml.write($stdout, 2)
这是不需要
<CuePoint>
<Time>
15359
</Time>
<Type>
event
</Type>
<Name>
inst_50
</Name>
</CuePoint>
<CuePoint>
<Time>
16359
</Time>
<Type>
event
</Type>
<Name>
inst_50
</Name>
</CuePoint>
的,因为它在只有文本的标签内容中插入了空格。即名称标签的内容现在是“\n inst_50\n”或其他内容。这会导致读取 xml 的应用程序崩溃。
有谁知道如何按照我想要的方式格式化输出文件?
感谢您的任何建议,最大
编辑 - 我刚刚通过另一篇 StackOverflow 帖子在 ruby-forum 上找到了答案: http://www .ruby-forum.com/topic/195353
formatter = REXML::Formatters::Pretty.new
formatter.compact = true
File.open(@xml_file,"w"){|file| file.puts formatter.write(xml.root,"")}
这会产生类似的结果
<CuePoint>
<Time>33997</Time>
<Type>event</Type>
<Name>inst_45_off</Name>
</CuePoint>
<CuePoint>
<Time>34080</Time>
<Type>event</Type>
<Name>inst_45</Name>
</CuePoint>
There's no extra line between the CuePoint Tags 但这对我来说很好。我将这个问题留在这里,以防其他人偶然发现它。
I'm building an xml doc with REXML, and want to output to text in a particular way. The doc is a list of CuePoint tags, and the ones that i've generated with Element.new and add_element are all mushed together into a single line like this: (stackoverflow has split them over two lines here but imagine the following is all on one line):
<CuePoint><Time>15359</Time><Type>event</Type><Name>inst_50</Name></CuePoint><CuePoint><Time>16359</Time><Type>event</Type><Name>inst_50</Name></CuePoint>
When i save them out to file, i want them to look like this:
<CuePoint>
<Time>15359</Time>
<Type>event</Type>
<Name>inst_50</Name>
</CuePoint>
<CuePoint>
<Time>16359</Time>
<Type>event</Type>
<Name>inst_50</Name>
</CuePoint>
I tried passing the .write function a value of 2, to indent them: this produces the following:
xml.write($stdout, 2)
produces
<CuePoint>
<Time>
15359
</Time>
<Type>
event
</Type>
<Name>
inst_50
</Name>
</CuePoint>
<CuePoint>
<Time>
16359
</Time>
<Type>
event
</Type>
<Name>
inst_50
</Name>
</CuePoint>
This is unwanted because it has inserted whitespace into the contents of the tags which just have text. ie the contents of the Name tag is now "\n inst_50\n " or something. This is going to blow up the app that reads the xml.
Does anyone know how i can format the output file the way i want it?
Grateful for any advice, max
EDIT - I just found the answer on ruby-forum, via another StackOverflow post: http://www.ruby-forum.com/topic/195353
formatter = REXML::Formatters::Pretty.new
formatter.compact = true
File.open(@xml_file,"w"){|file| file.puts formatter.write(xml.root,"")}
This produces results like
<CuePoint>
<Time>33997</Time>
<Type>event</Type>
<Name>inst_45_off</Name>
</CuePoint>
<CuePoint>
<Time>34080</Time>
<Type>event</Type>
<Name>inst_45</Name>
</CuePoint>
There's no extra line between the CuePoint tags but that's fine for me. I'm leaving this question up here in case anyone else stumbles across it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将格式化程序的紧凑属性设置为true,但只能通过首先设置单独的格式化程序对象,然后使用它来进行写入而不是调用文档自己的写入方法来做到这一点。
You need to set the formatter's compact property to true, but you can only do that by setting a separate formatter object first, then using that to do your writing rather than calling the document's own write method.