Home » Questions » Computers [ Ask a new question ]

How can I diff two XML files?

How can I diff two XML files?

On Linux, how could I generate a diff between two XML files?

Asked by: Guest | Views: 384
Total answers/comments: 5
Guest [Entry]

"One approach would be to first turn both XML files into Canonical XML, and compare the results using diff. For example, xmllint can be used to canonicalize XML.

$ xmllint --c14n one.xml > 1.xml
$ xmllint --c14n two.xml > 2.xml
$ diff 1.xml 2.xml

Or as a one-liner.

$ diff <(xmllint --c14n one.xml) <(xmllint --c14n two.xml)"
Guest [Entry]

"Tried to use @Jukka Matilainen's answer but had problems with white-space (one of the files was a huge one-liner).
Using --format helps to skip white-space differences.

xmllint --format one.xml > 1.xml
xmllint --format two.xml > 2.xml
diff 1.xml 2.xml

Note: Use vimdiff command for side-by-side comparison of the xmls."
Guest [Entry]

"My Python script xdiff.py for comparing XML files ignores differences in
whitespace or attribute order (in contrast to element order).

In order to compare two files 1.xml and 2.xml, you would run the script as follows:

xdiff.py 1.xml 2.xml

In the OP's example, it would output nothing and return exit status 0 (for no
structural or textual differences).

In cases where 1.xml and 2.xml differ structurally, it mimics the unified
output of GNU diff and returns exit status 1. There are various options for controlling the output, such
as -a for outputting all context, -n for outputting no context, and -q for
suppressing output altogether (while still returning the exit status)."
Guest [Entry]

"If anyone stumbles upon this and is a developer and knows programming languages, then you can also check XML difference using XMLUnit in C# or JAVA.
For checking how does it show's difference you can try this online XML difference checker tool
C# Sample Code to check difference
string control = ""<a><b attr=\""abc\""></b></a>"";
string test = ""<a><b attr=\""xyz\""></b></a>"";

var myDiff = DiffBuilder.Compare(Input.FromString(control))
.WithTest(Input.FromString(test))
.Build();

Assert.IsFalse(myDiff.HasDifferences(), myDiff.ToString());"
Guest [Entry]

I use Beyond Compare to compare all types of text based files. They produce versions for Windows and Linux.