Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intelligent Way to Diff XML

Tags:

diff

xml

I'm using a program that acts like a platform which creates its instances of objects it uses and stores them in xml format in a database. This program has a way to create a "dump" of these objects into an xml collection.

I'm looking to do a diff of dumps between our production environment and our development environment, to be able to determine any deltas between them.

Since the dumps depend on the order of items in the db, a normal diff doesn't make sense. What I need is a way for the xml elements to be sorted in both dumps before the comparison, so that the diff is more intelligent and reflects the deltas more accurately.

For example:

File 1:

<?xml version="1.0" encoding="UTF-8"?>
<foo>
     <bar1>stuff1</bar1>
     <bar2>stuff2</bar2>
</foo>

File 2:

<?xml version="1.0" encoding="UTF-8"?>
<foo>
     <bar2>stuff2</bar2>
        <bar1>stuff1</bar1>
</foo>

I'm looking for a way where the diff of the above files would result in no difference, as the only differences are white space and sub order of the elements of foo.

Any ideas on how to accomplish this with large files?

The following works with small files:

Using http://prettydiff.com/ I can accomplish this by making sure the option of "Sort markup pieces" was selected. After which I was able to diff the above and it showed things as a match.

I need to do something like this locally that will work for files that can be considerable in size.

like image 520
James Oravec Avatar asked Dec 02 '16 15:12

James Oravec


People also ask

How to compare two XML data structures for differences?

Be careful, the data will be accessible to everyone, please do not save sensitive data. This small tool allows to compare two XML data structures, and visualize the diff. Copy and paste, drag and drop a XML file or directly type in the editors above, and then click on "Compare" button they will be compared if the two XML are valids.

Is this XML compare tool free?

Is this XML Compare Tool free? Yes, this service is free and we don't save any data unless you explicitly ask us to save. What are other features of this XML Comparator? This tool can also beautify or format your XML data. Right click on the pane you want to beautify/prettify & select Format Document.

How to find the semantic differences between two XML strings?

This free online tool will allows you to do this easily. XML diff tool makes a semantic comparison, it compares every attribute–value pairs of objects. It compares each node according to their position in the arrays. It sorts and formats the XML strings in order to find the semantic differences instead of just the text ones.

How to beautify or format Your XML data?

This tool can also beautify or format your XML data. Right click on the pane you want to beautify/prettify & select Format Document. This is very helpful when you are comparing data from web services that often minify XML files. This XML tool can show syntax highlighting for your comfort.


1 Answers

Giving this some thought, I figured if there was a way to sort the xml files from command line, then I could just do a diff against the files.

My follow up googles to my thought lead me to the following: https://superuser.com/questions/79920/how-can-i-diff-two-xml-files

In the above, the key was the canonical xml sort. Since I'm using a mac, the above gave me the following for my problem, which worked me:

$ xmllint --c14n File1.xml > 1.xml
$ xmllint --c14n File2.xml > 2.xml
$ diff 1.xml 2.xml

The above should also work for you if you are on linux, or are using something like cygwin installed/setup with windows.

like image 90
James Oravec Avatar answered Oct 02 '22 08:10

James Oravec