Ruby:无法使用 Libxml-ruby 保存文档
来自 libxml-ruby API 文档 (http://libxml.rubyforge.org/rdoc/index.html html),在 LibXML::XML::Document 下,我尝试了以下操作:
filename = 'something.xml'
stats_doc = XML::Document.new()
stats_doc.root = XML::Node.new('root_node')
stats_doc.root << XML::Node.new('elem1')
stats_doc.save(filename, :indent => true, :encoding => 'utf-8')
... 这导致了此错误:(
parse_stats.rb:26:in `save': can't convert String into Integer (TypeError)
其中上面块中的最后一行是第 26 行)。
我尝试将文件名更改为整数,这给了我这样的结果:
parse_stats.rb:26:in `save': wrong argument type Fixnum (expected String) (TypeError)
所以我认为我需要使用字符串,但字符串似乎不起作用。 由于我似乎无法在 Google 之外找到任何 libxml-ruby 的示例,所以我不知所措。 任何帮助,或者链接到任何在线示例,我都将非常感激,在其中我可以看到 libxml-ruby 如何用于创建 XML 文档。
libxml-ruby 1.1.3 红宝石 1.3.1 红宝石 1.8.7
From the libxml-ruby API docs (http://libxml.rubyforge.org/rdoc/index.html), under LibXML::XML::Document, I tried the following:
filename = 'something.xml'
stats_doc = XML::Document.new()
stats_doc.root = XML::Node.new('root_node')
stats_doc.root << XML::Node.new('elem1')
stats_doc.save(filename, :indent => true, :encoding => 'utf-8')
... which resulted in this error:
parse_stats.rb:26:in `save': can't convert String into Integer (TypeError)
(where the last line in the block above was line 26).
I tried changing the file name to an integer, which gave me this:
parse_stats.rb:26:in `save': wrong argument type Fixnum (expected String) (TypeError)
So I gathered that I need to use a string, but strings don't seem to work. Since I seem to be unable to find any examples of libxml-ruby in action off Google, I'm at a loss. Any help would be very appreciated, or links to any online example where I can see how libxml-ruby is used for creating XML documents.
libxml-ruby 1.1.3
rubygems 1.3.1
ruby 1.8.7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来问题出在编码上。 尝试 XML::Encoding::UTF_8 而不是“utf” -8”。
Seems that the problem is with encoding. Try XML::Encoding::UTF_8 instead of "utf-8".