Use C++ to compare changes in XML -



Use C++ to compare changes in XML -

i have 2 big xml files have same schema different entries. each day entries alter , want able find:

entry appears in file not file b entry appears in file b not file a entry appears in both file , b

i'm new programming , i'm having hard time understanding efficient way approach this. using (trillions of) loops key this? ideas on route solve problem appreciated. give thanks you.

example shortened xml file:

<?xml version="1.0" encoding="iso-8859-1" ?> <site_entries> <entry> <id><![cdata[946757316]]></id> <url><![cdata[http://www.site.co.uk/cgi-bin/tr.cgi?tid=752276]]></url> <content><![cdata[specialized dolce sport 27 speed]]></content> <title><![cdata[bike]]></title> <price><![cdata[£600]]></price> <date><![cdata[01-aug-13]]></date> <display_reference><![cdata[214683-50142933_370647]]></display_reference> <location><![cdata[city of london]]></location> <category><![cdata[bike]]></category> </entry> <entry> <id><![cdata[90007316]]></id> <url><![cdata[http://www.site.co.uk/cgi-bin/tr.cgi?tid=70952276]]></url> <content><![cdata[giant sport offroad bike]]></content> <title><![cdata[bike]]></title> <price><![cdata[£100]]></price> <date><![cdata[11-aug-15]]></date> <display_reference><![cdata[2146433-50142933_370647]]></display_reference> <location><![cdata[city of london]]></location> <category><![cdata[bike]]></category> </entry> </site_entries>

edit: can't rely on entires beingness in right order across files.

here illustration of how can work using pugixml.

for purposes of test xml files stored in std::istringstream objects, can replaced std::ifstream objects read files.

#include <set> #include <string> #include <sstream> #include <iostream> #include <algorithm> #include "pugixml.hpp" #define con(m) std::cout << m << '\n' #define err(m) std::cerr << m << std::endl std::istringstream iss_a(r"~(<?xml version="1.0" encoding="iso-8859-1" ?> <site_entries> <entry> <id><![cdata[1]]></id> </entry> <entry> <id><![cdata[2]]></id> </entry> </site_entries>)~"); std::istringstream iss_b(r"~(<?xml version="1.0" encoding="iso-8859-1" ?> <site_entries> <entry> <id><![cdata[2]]></id> </entry> <entry> <id><![cdata[3]]></id> </entry> </site_entries>)~"); using str_set = std::set<std::string>; int main() { pugi::xml_document doc; str_set a; doc.load(iss_a); // utilize doc.load_file() in real code // fill set ids file for(auto&& node: doc.child("site_entries").children("entry")) a.emplace(node.child("id").text().as_string()); str_set b; doc.load(iss_b); // fill set b ids file b for(auto&& node: doc.child("site_entries").children("entry")) b.emplace(node.child("id").text().as_string()); // utilize <algorithms> library str_set b_from_a; std::set_difference(a.begin(), a.end(), b.begin(), b.end() , std::inserter(b_from_a, b_from_a.begin())); str_set a_from_b; std::set_difference(b.begin(), b.end(), a.begin(), a.end() , std::inserter(a_from_b, a_from_b.begin())); str_set a_and_b; std::set_intersection(a.begin(), a.end(), b.begin(), b.end() , std::inserter(a_and_b, a_and_b.begin())); for(auto&& v: a) con("a : " << v); con(""); for(auto&& v: b) con("b : " << v); con(""); for(auto&& v: b_from_a) con("b_from_a: " << v); con(""); for(auto&& v: a_from_b) con("a_from_b: " << v); con(""); for(auto&& v: a_and_b) con("a_and_b : " << v); con(""); }

output:

a : 1 : 2 b : 2 b : 3 b_from_a: 1 a_from_b: 3 a_and_b : 2

references:

std::set_difference

std::set_intersection

c++ xml xml-parsing diff

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -