dimanche 19 avril 2015

Use pugiXML to rename nodes based on a std::map

I'm new to C++ but I am trying to define a standard set of node names and then map to them.


For example my standard import / output schema is this:



<data>
<entry>
<id>1</id>
<description>Test</description>
</entry>
</data>


However sometimes my XML import will be named differently so I want to create a map so it still outputs in the above format, even if the input file has this naming convention:



<data>
<entry>
<id>1</id>
<content>Test</content>
</entry>
</data>


This code is my best guess based on the documentation and help I've got, but I have got stuck in trying to complete it:



#include "pugi/pugixml.hpp"

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

int main()
{

// Define mappings, default left - map on the right
const std::map<std::string, std::string> tagmaps
{
{"id", "id"}
, {"description", "content"}
};

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 (auto& eb: mapb) {
// change node name if mapping found
if((found = tagmaps.find(n.name())) != tagmaps.end()) {
n.set_name(found->second.c_str());
}

}

}


This code would ideally allow the xml to be formatted either way but always output the same. Any help would be really appreciated.


Aucun commentaire:

Enregistrer un commentaire