我无法让 xmlSimple 在我的 .rb 文件中正确运行。我正在使用xmlSimple.xmlIn(filename);
,但是,找到正确的文件似乎有错误。我已将文件移动到 bin 并且文件存在,意思是filename.exists? = true
. 关于可能的错误来源的任何想法?谢谢!
-编辑-让我添加此信息;我对 Ruby 很陌生,很有可能我的方法或语法完全错误,这是我在 .rb 文件中的代码:
require 'xmlsimple'
file_name = 'xmldatatest.xml'
paragraph_str = 0
file = File.open(file_name) # takes XML Data and creates a file of the data
File.open(file_name, "w+") do |f| # open file for update
lines = f.readlines # read into array of lines
lines.each do
|it|
# modify lines
it.gsub!(/\n/, '')
it.gsub!('<p>', '')
it.gsub!('</p>', '')
it.gsub!('\"Paragraph.\"', 'Paragraph')
if ((it.include? ('Paragraph')) == 1)
paragraph_str += 1
end
while paragraph_str > 0 do
initial_value = paragraph_str
if ((paragraph_str == initial_value))
it.gsub!(/Paragraph/, '<p>')
paragraph_str -= 1
else
it.gsub!(/Paragraph/, '</p><p>')
paragraph_str -= 1
end
end
f.print lines # write out modified lines
end
end
File.open(file_name, 'a') {|f| f.puts "</p>" }
ref = XmlSimple.xml_in(file_name)
该程序的目的是去除原始 XML 文件中的所有转义字符,然后替换<p>
and</p>
标记中的每个“Paragraph#”节点。之后,将使用XmlSimple.Xml_in(filename)
. 任何建议或更正都非常感谢。