How to compare XML files
I had a similar problem and I eventually found: http://superuser.com/questions/79920/how-can-i-diff-two-xml-files
That post suggests doing a canonical XML sort then doing a diff. The following should work for you if you are on Linux, Mac, or if you have Windows with something like Cygwin installed:
$ xmllint --c14n FileA.xml > 1.xml
$ xmllint --c14n FileB.xml > 2.xml
$ diff 1.xml 2.xml
Compare two xml files ignoring certain elements using xpath in Java
You need to transform both files into a form where they compare equal, by removing the elements you want to ignore. You would typically do this using XSLT. After the transformation you could either compare the results using the XPath 2.0 function deep-equal(), or serialise both documents as canonical XML and compare the files at the character or binary level.
UPDATE
Thanks for explaining the question more clearly.
I would do this by running XQuery Update to delete the nodes selected by the path expression, and then comparing the resulting documents either using fn:deep-equal(), or by doing canonical serialization and comparing the resulting lexical forms.
As an alternative to XQuery Update you could use xmlstarlet or Saxon's Gizmo tool.
But it might depend on what you want from the comparison. The above is fine if you want a yes/no answer, but getting details of the differences is more difficult. You could write your own query to find the differences, or use a tool such as DeltaXML.
How to print all differences when comparing two xml files using XMLUnit2
The Diff
object you get as a result of the comparison contains all differences and you can access them with diff.getDifferences()
. The toString
method of Diff
that you invoke when you print
it only prints the first difference.
So if you want to print all differences you'd do something like
for (Difference d : diff.getDifferences()) {
System.err.println(d);
}
For more control over the output take a look at ComparisonFomatter
and Difference
's one-arg toString
method.
Best way to compare 2 XML documents in Java
Example:
public class SomeTest extends XMLTestCase {
@Test
public void test() {
String xml1 = ...
String xml2 = ...
XMLUnit.setIgnoreWhitespace(true); // ignore whitespace differences
// can also compare xml Documents, InputSources, Readers, Diffs
assertXMLEqual(xml1, xml2); // assertXMLEquals comes from XMLTestCase
}
}
How to compare two XML files and display the differences in html format using Ruby
An alternate solution using Diffy to do the comparison. It is a flat string comparison but it does have an html formatted output.
require 'diffy'
puts Diffy::Diff.new('./docOne.xml', './docTwo.xml', :source => 'files').to_s(:html)
#Example output
<div class="diff">
<ul>
<li class="unchanged"><span><root></span></li>
<li class="del"><del>test</del></li>
<li class="unchanged"><span><foo></foo></span></li>
<li class="ins"><ins><bar></bar></ins></li>
<li class="unchanged"><span></root></span></li>
</ul>
</div>
Related Topics
Is There a Reason Image.Fromfile Throws an Outofmemoryexception for an Invalid Image Format
How to Remove Whitespace on Merge
Predefined Type 'System.Valuetuple'2' Is Not Defined or Imported
How to Get the Computer Name in .Net
How to Return a Value from a Form in C#
How to Copy Data to Clipboard in C#
What Strategies and Tools Are Useful for Finding Memory Leaks in .Net
How to Unserialize PHP Serialized Array/Variable/Class and Return Suitable Object in C#
How Do Prefix (++X) and Postfix (X++) Operations Work
Marshal C++ Struct Array into C#
Kill Some Processes by .Exe File Name
Remove HTML Tags from String Including &Nbsp in C#
Asp.Net: Invalid Postback or Callback Argument
Programmatically Add Column & Rows to Wpf Datagrid