MATLAB Report Generator

Comparing XML Files

This example shows you how to use MATLAB® Report Generator™ to perform an XML comparison of two XML text files, and view the differences in the resulting report.

Introduction

This example compares two XML text files that describe a catalytic converter test. The XML files record information about the environment when the test was performed, such as time and temperature, as well as the results of the tests that were performed. You can use XML comparison to explore the changes between files.

Raw Test Files

The test files are stored as XML. To view their contents, run the following commands:

type('xmldemo_test_run1.xml')

type('xmldemo_test_run2.xml')
<testlog name="automatedtest" testversion="1.1">
    <test>
        <testname>Catalysts</testname>   
        <header>
            <date>12 August 2007</date>
            <time zone="GMT">10:43:01</time>
            <description>Catalyst checks</description>
            <test-id>74.33</test-id>
        </header>
        <environment>
            <temperature unit="celsius">21</temperature>
            <humidity unit="percent">71</humidity>
            <pressure unit="millibars">1032</pressure>
            <weather>rain</weather>
        </environment>
        <vehicle>
            <chassisnumber>1024GHXHHY1672A</chassisnumber>
            <engine>
                <capacity unit="ml">1896</capacity>
                <cylinders>4</cylinders>
                <fuel>gasoline</fuel>
                <ECUVersion>v16.1.13.1</ECUVersion>
            </engine>
        </vehicle>
        <results>
            <warmup>pass</warmup>
            <coolingfan>activated</coolingfan>
            <lambda>pass</lambda>
            <hydrocarbons>pass</hydrocarbons>
            <carbonmonoxide>pass</carbonmonoxide>
            <fastidle>pass</fastidle>
        </results>
    </test>
</testlog>

 

<testlog name="automatedtest" testversion="1.2">
    <test>
        <testname>Catalysts</testname>            
        <header>
            <date>15 August 2008</date>
            <time zone="GMT">14:02:34</time>
            <description>Catalyst checks</description>
            <test-id>74.33</test-id>
        </header>
        <environment>
            <weather>rain</weather>
            <temperature unit="celsius">24</temperature>
            <humidity unit="percent">79</humidity>
            <pressure unit="millibars">1201</pressure>
        </environment>
        <vehicle>
            <chassisnumber>1024GHXHHY1672A</chassisnumber>
            <engine>
                <capacity unit="ml">1896</capacity>
                <cylinders>4</cylinders>
                <fuel>gasoline</fuel>
            </engine>
            <ECUVersion>v16.1.13.1</ECUVersion>
        </vehicle>
        <results>
            <warmup>pass</warmup>
            <coolingfan>activated</coolingfan>
            <lambda>fail</lambda>
            <rerun>
                <lambda>pass</lambda>
            </rerun>
            <hydrocarbons>pass</hydrocarbons>
            <carbonmonoxide>pass</carbonmonoxide>
            <fastidle>pass</fastidle>
        </results>
    </test>
</testlog>

Compare The XML Files

Use the Comparison Tool to compare the two XML files.

visdiff('xmldemo_test_run1.xml', 'xmldemo_test_run2.xml')

Understand The Results

The Comparison Tool shows only differences between the two XML files as a hierarchy, via two tree controls. The report does not display sections of the XML files which are identical. For example, both xmldemo_test_run1.xml and xmldemo_test_run2.xml contain an element called "testname" that contains the same text, "Catalysts", in both files. This item is not included in the comparison report because it is identical in both files.

Click the "Expand All" button on the Comparison Tool to expand the report trees and view every difference found by the analysis.

Colors indicate if items are modified (pink items) or unmatched (green items, found only on one side).

View Environment Changes

The values of environmental data (temperature, humidity and pressure) are different in each file, and you can view these elements, and the changed text content, in the report. Click the "environment" item in one tree to automatically select it in the other.

Click temperature, humidity and pressure in turn, and view the different values on each side in the Parameter Values pane at the bottom of the report.

Note that these environment elements have changed order. Click the "weather" element in the comparison report. The report highlights the weather element on both sides. In this case, the "weather" text content is the same in both files, but its location has changed within the environment node. The highlighting shows you that in xmldemo_test_run1.xml the "weather" element is located at the end of the "environment" element. In xmldemo_test_run2.xml it has moved to the beginning of the "environment" element.

View Header Changes

Under the "header" element, you can see the that the contents of some child elements have changed.

Click the "time" element to view the item selected on both sides of the report, and view the different parameter values in the lower pane.

Observe the "date" element is green, showing it is "Unmatched". The Chawathe algorithm cannot match the "date" elements because the text content is different and there are no attributes to compare. The other elements in the file have attributes which cause them to have a higher "score" according to the Chawathe matching rules, so they match (e.g. the "zone" attribute of the "time" element in the XML file:

% <time zone="GMT">14:02:34</time>

View Vehicle Changes

In xmldemo_test_run1.xml, the "ECUVersion" parameter is part of the "engine" grouping, which is part of the "vehicle" element. In xmldemo_test_run2.xml, the "ECUVersion" parameter has moved out of "engine" and into "vehicle". Click "ECUVersion" to see the different position on each side.

Under the "results" element there is an element "lambda". In xmldemo_test_run1.xml this has the value "pass". In xmldemo_test_run2.xml this has the value "fail", and is followed by an additional node "rerun" containing another element "lambda" with value "pass". Since "rerun" exists only in xmldemo_test_run2.xml it is shown as unmatched. The Chawathe algorithm regards the two instances of lambda with value "pass" as being the best match, and so the element lambda in xmldemo_test_run1.xml is shown as having moved to within "rerun" in xmldemo_test_run2.xml.

Further Information

XML Comparison documentationXML Comparison documentation