This is my C++ code which is meant to do the following:
- Compare a set of XML files and see what is different between them
- Dump out the node if it is new (in B.xml but not A.xml)
- Scan that node and associate tags to types of information using map
- Do work on the data depending on what type it is
I'm pretty happy with how steps 1-2 work but 3-4 I feel I may have implemented poorly. Any help would be appreciated. My main concern is I have to map the tag even if if already matches, such as the id, when really it would be good to define a map only if it's different such as description.
My code:
#include "pugi/pugixml.hpp"
#include <iostream>
#include <string>
#include <map>
int main()
{
// This map relates the type of content to the tag name in the XML file
const std::map<std::string, std::string> tagMap {
{"id", "id"}, {"description", "content"}, {"url", "web_address"}, {"location", "location"}
};
pugi::xml_document doca, docb;
std::map<std::string, pugi::xml_node> mapa, mapb;
for (auto& node: doca.child("data").children("entry")) {
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 added nodes
for (auto& eb: mapb) {
// Loop through Tag map to see if we can find tags named "id, content, web_address or location" in the node returned
for (auto& kv : tagMap) {
// For each result, assign the value of that tag to the type of content
// For example: description = Testing!
kv.first = eb.second.child_value(kv.second.c_str());
// If it's an ID...
if (kv.first == "id") {
// Do work on ID value (i.e check if it's unique)
}
if (kv.first == "description") {
// Do work on Description data (I.e Trim it)
}
if (kv.first == "url") {
// Do work on URL data (I.e validate it)
}
if (kv.first == "location") {
// Do work on location data
}
}
}
}
Example input file:
<data>
<entry>
<id>1</id>
<content>Description</content>
<web_address>http://ift.tt/1yK4hfs;
<location>England</location>
<unrelated>Test</unrelated>
<not_needed>Test</not_needed>
</entry>
..
</data>
Aucun commentaire:
Enregistrer un commentaire