PDA

View Full Version : Xml compare using QDomElement



anbu01
28th June 2015, 09:06
Hi all,
I need to compare two xml files.So far i tried using QDomDocument and QDomNode::operator() == but i failed since the book properties can be in any order.All i need to do is compare both file and say they are same or not.For example:-

File1.xml


<Book class="B1" name="ABC">
<property name="Price">
<int>85</int>
</property>
<property name="author">
<string>Hypo</string>
</property>
</widget>

File2.xml


<Book class="B1" name="ABC">
<property name="Price">
<int>85</int>
</property>
<property name="author">
<string>Hypo</string>
</property>
</widget>
Can anyone guide me on this.How can i proceed?

anda_skoa
28th June 2015, 11:49
You could traverse both DOM tree and on each step you check that the two nodes have the same type.
If the node is an element, compare names and attribute maps.
If the node is a text element, also compare text.

Or read both files into DOM trees and then write them to two temporay files or buffers and then compare their content.

Cheers,
_

anbu01
28th June 2015, 12:33
Thanks for your reply but what happens if my child nodes are in different order for Example:-

File1.xml

<Book class="B1" name="ABC">
<property name="Price">
<int>85</int>
</property>
<property name="author">
<string>Hypo</string>
</property>
</widget>

File2.xml


<Book class="B1" name="ABC">
<property name="author">
<string>Hypo</string>
</property>
<property name="Price">
<int>85</int>
</property>
</widget>

anda_skoa
28th June 2015, 13:46
Well, if that can happen, you'll have to load the data into program data structures and compare those.

Cheers,
_

anbu01
3rd July 2015, 09:42
@adoka
Thanks that will be the ideal choice.