dimanche 19 avril 2015

Port C++ to NodeJS - Compare XML files

I have some C++ code to compare XML files. I'm looking at moving my application to nodeJS because it's mostly based on JSON. However I'm concerned that nodeJS will struggle with this bit so I want to try it. This is how far I have got.


I'm new to c++ and nodejs and struggling to find the equivalent way to load the data into the map to do the comparison in nodejs like I did in c++ so any help would be really appreciated.



// Access FileSystem
ar fs = require('fs');
// Add XML2JS module
var xml2js = require('xml2js');

// Assume this returns a fully qualified XML file path
var filePath = GetFilePath();
try {
var fileData = fs.readFileSync(filePath, 'ascii');

var parser = new xml2js.Parser();
parser.parseString(fileData.substring(0, fileData.length), function (err, result) {
var json = JSON.stringify(result);
});

console.log("File '" + filePath + "/ was successfully read.\n");
} catch (ex) {
console.log("Unable to read file '" + filePath + "'.");
console.log(ex);
}


This is the original code I had in C++:



#include "pugi/pugixml.hpp"

#include <iostream>
#include <string>
#include <map>

int main()
{

pugi::xml_document doca, docb;
std::map<std::string, pugi::xml_node> mapa, mapb;

if (!doca.load_file("a.xml") || !docb.load_file("b.xml")) {
std::cout << "Can't find input files";
return 1;
}

for (auto& node: doca.child("jobsite_vacancies").children("job")) {
const char* id = node.child_value("id");
mapa[id] = node;
}

for (auto& node: docb.child("data").children("entry")) {
const char* idcs = node.child_value("id");
if (!mapa.erase(idcs)) {
mapb[idcs] = node;
}
}

for (auto& ea: mapa) {
std::cout << "Removed:" << std::endl;
ea.second.print(std::cout);
}

for (auto& eb: mapb) {
std::cout << "Added:" << std::endl;
eb.second.print(std::cout);
}

}

Aucun commentaire:

Enregistrer un commentaire