我无法正确编辑XML文件。我想删除某些元素,然后添加新元素。漂亮的打印文件与REXML
<project>
<option>
<name>foo</name>
<state>0</state>
</option>
<option>
<name>bar</name>
<state>foo/apple</state>
<state>foo/orange</state>
</option>
</project>
我想删除state
的苹果和橘子和葡萄插入,柠檬和酸橙。我曾尝试使用此代码:
#!/usr/bin/ruby -w
require 'fileutils'
require 'rexml/document'
require 'find'
include REXML
path = 'C:\Users\GustavWi\Documents\Gustav\help.xml'
xmlfile = File.new(path)
xmldoc = Document.new(xmlfile)
str_new_elements =["grape","lemon","lime"]
xmldoc.elements.each("project/option") do |parent|
if parent.elements['name'].text == 'bar'
parent.elements.each do |element|
str = element.text.split('/')
if str[0] == 'foo'
parent.delete_element(element)
end
end
str_new_elements.each do |dir|
state = Element.new("state")
state.text = dir
parent.add_element(state)
end
end
end
File.open(path,"w") do |data|
xmldoc.write(data)
end
的问题是,输出是:
<project>
<option>
<name>foo</name>
<state>0</state>
</option>
<option>
<name>bar</name>
<state>grape</state><state>lemon</state><state>lime</state></option>
</project>
的问题是空行和新元素的缺失缩进。
我使用的是Ruby 1.8.6,这可能是一个问题,但我还没有看到任何信息,这是1.8.6中的问题。
几乎同样的问题,可以在著作“编程的Ruby和Pragmatic Programmers'指南”中可以看出726页
对于它的价值,我得到了一个完全不同的输出,在Ruby 1.8.6(和2.1)上运行您的代码。你可以用粘贴的代码重现问题吗? –
我认为这里的问题是'xmldoc.write(data,0)',它从输出中删除缩进。如果你摆脱了0,那么你会得到更像问题中的例子。 – Max