5

What's the easiest way to see the difference between two xml files using?

I looked into Hpricot and Nokogiri but couldn't find any good comparison methods. I've also looked into unix tools like diffxml, but would rather use something in ruby.

Anybody got any ideas?

Chris McCauley
  • 25,824
  • 8
  • 48
  • 65
jpoz
  • 2,257
  • 1
  • 23
  • 29
  • 2
    I don't think this is a duplicate question. Diffing trees and diffing sequences are different (though abstractly related) problems. – Lucas Wiman Aug 13 '11 at 01:57

4 Answers4

1

Try the equivalent-xml gem. It can tell you if two XML documents are semantically equivalent

1

What about diff.rb ?
You export your two xml documents to arrays and get the diff with that library.

Damien MATHIEU
  • 31,924
  • 13
  • 86
  • 94
0

I found that was easier to use the Java library XMLUnit than any native solution

require 'java'
require '/usr/share/java/xmlunit.jar'
java_import org.custommonkey.xmlunit.Diff

puts Diff.new("<a></a>", "<a/>").similar
puts Diff.new("<a/>", "<b/>").similar

Of course, this means you have to use JRuby instead of CRuby, but that wasn't a significant obstacle in my case.

Bryan Larsen
  • 9,468
  • 8
  • 56
  • 46
0

Another option is to regularize your xml by round tripping it through xml-simple, and then doing an array diff on the lines of the output:

require 'rubygems'
require 'xmlsimple'
require 'diff'

def regularize(xml)
  XmlSimple.xml_out(XmlSimple.xml_in(xml))
end

def diff_xml(a,b)
  Diff.new(regularize(a).split("\n"), regularize(b).split("\n"))
end

puts diff_xml("<doc><a/></doc>", "<doc><a></a></doc>").diffs.empty?
puts diff_xml("<doc><a/></doc>", "<doc><b/></doc>").diffs.empty?
Bryan Larsen
  • 9,468
  • 8
  • 56
  • 46