我想在Ruby中创建一个2x2表,以便输出一些XML文件的并排比较。第一行只是标题,下一行是文件。
答案 0 :(得分:0)
根据我的理解,这样一个非常简单的脚本可以完成这项工作,但我可能错过了你的问题:
# Assuming your xml files are xml1.xml and xml2.xml, this will create an HTML file called result.html containing the HTML table
html = ""
html += "<html>"
html += "<table>"
html += "<tr><td>First XML File</td><td>Second XML file</td></tr>"
html += "<tr>"
html += "<td>"
html += File.read("xml1.xml", :encoding => "UTF-8").encode(:xml => :text)
html += "</td>"
html += "<td>"
html += File.read("xml2.xml", :encoding => "UTF-8").encode(:xml => :text)
html += "</td>"
html += "</tr>"
html += "</table>"
html += "</html>"
%x(echo '#{html}' >> "result.html")
如果你想拥有漂亮的缩进,你可以使用内置的ruby核心REXML :: Document。您还必须将漂亮缩进的XML嵌套在预标记中,以便显示良好:
require "rexml/document"
doc1 = REXML::Document.new File.read("xml1.xml", :encoding => "UTF-8")
out1 = ""
doc1.write(out1, 2)
doc2 = REXML::Document.new File.read("xml2.xml", :encoding => "UTF-8")
out2 = ""
doc2.write(out2, 2)
html=""
html += "<html>"
html += "<table>"
html += "<tr><td>First XML File</td><td>Second XML file</td></tr>"
html += "<tr>"
html += "<td><pre>"
html += out1.encode(:xml => :text)
html += "</pre></td>"
html += "<td><pre>"
html += out2.encode(:xml => :text)
html += "</pre></td>"
html += "</tr>"
html += "</table>"
html += "</html>"
%x(echo '#{html}' >> "result.html")