Ruby:libxml-ruby 并添加格式良好的同级节点

发布于 2024-07-16 05:35:15 字数 1334 浏览 14 评论 0原文

给定我现有的 XML (test.xml):

<root>
  <element>
    <child id="1" />
    <child id="2" />
    <child id="3" />
  </element>
</root>

和我的 ruby​​ 代码:

require 'rubygems'
require 'xml'

parser = XML::Parser.file("test.xml")
doc = parser.parse

target = doc.find('/*/element')
target << child = XML::Node.new('child')
child['id'] = '4'

XML.indent_tree_output = true
doc.save(filename, :indent => true, :encoding => XML::Encoding::UTF_8)

我的问题是它格式化输出如下:

<root>
  <element>
    <child id="1" />
    <child id="2" />
    <child id="3" />
  <child id="4" /></element>
</root>

... 后续添加如下所示:

<root>
  <element>
    <child id="1" />
    <child id="2" />
    <child id="3" />
  <child id="4" /><child id="5" /><child id="6" /></element>
</root>

想要是这样的:

<root>
  <element>
    <child id="1" />
    <child id="2" />
    <child id="3" />
    <child id="4" />
    <child id="5" />
    <child id="6" />
  </element>
</root>

。 ..但是我怎样才能得到它呢?

Given my existing XML (test.xml):

<root>
  <element>
    <child id="1" />
    <child id="2" />
    <child id="3" />
  </element>
</root>

And my ruby code:

require 'rubygems'
require 'xml'

parser = XML::Parser.file("test.xml")
doc = parser.parse

target = doc.find('/*/element')
target << child = XML::Node.new('child')
child['id'] = '4'

XML.indent_tree_output = true
doc.save(filename, :indent => true, :encoding => XML::Encoding::UTF_8)

My problem is that it formats the output like this:

<root>
  <element>
    <child id="1" />
    <child id="2" />
    <child id="3" />
  <child id="4" /></element>
</root>

... with subsequent additions looking like this:

<root>
  <element>
    <child id="1" />
    <child id="2" />
    <child id="3" />
  <child id="4" /><child id="5" /><child id="6" /></element>
</root>

What I WANT is this:

<root>
  <element>
    <child id="1" />
    <child id="2" />
    <child id="3" />
    <child id="4" />
    <child id="5" />
    <child id="6" />
  </element>
</root>

... but how do I get it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平定天下 2024-07-23 05:35:15

替换
parser = XML::Parser.file("test.xml")


parser = XML::Parser.file("test.xml", :options => XML::Parser::Options::NOBLANKS )

这是帮助

replace
parser = XML::Parser.file("test.xml")

with
parser = XML::Parser.file("test.xml", :options => XML::Parser::Options::NOBLANKS )

that's help

浮萍、无处依 2024-07-23 05:35:15

如果您使用的是 document.save,请确保将 indent 设置为 true,并确保设置了 XML.indent_tree_output。 像这样的事情:

XML.indent_tree_output = true
doc.save(filename, :indent => true, :encoding => XML::Encoding::UTF_8)

Rubyforge 不适合我,所以我无法在文档中验证它,但我认为两者都需要设置为 true 才能缩进和新行工作。

If you're using document.save then make sure you set indent to true, also make sure XML.indent_tree_output is set. Something like this:

XML.indent_tree_output = true
doc.save(filename, :indent => true, :encoding => XML::Encoding::UTF_8)

Rubyforge isn't working for me so I can't verify it in the documentation, but I think both need to be set to true for indenting and new lines to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文