问题
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. The code above gives me the following errors:
src/main.cpp:34:13: error: use of undeclared identifier 'found'
if((found = tagmaps.find(n.name())) != tagmaps.end()) {
^
src/main.cpp:34:34: error: use of undeclared identifier 'n'
if((found = tagmaps.find(n.name())) != tagmaps.end()) {
^
src/main.cpp:35:13: error: use of undeclared identifier 'n'
n.set_name(found->second.c_str());
^
src/main.cpp:35:24: error: use of undeclared identifier 'found'
n.set_name(found->second.c_str());
^
回答1:
The variables found
and n
are never declared. Declare those variables as the appropriate type before that loop so that section of code looks like:
EDIT: changed the code slightly, the if statement should check the value of found after it has been set.
pugi::xml_node found, n;
for (auto& eb: mapb) {
// change node name if mapping found
found = tagmaps.find(n.name());
if((found != tagmaps.end()) {
n.set_name(found->second.c_str());
}
}
Also, I presume n
should be set to a particular node inside the loop (at the moment it has no value). Consider renaming n
to something else to make it apparent what this variable should be holding.
来源:https://stackoverflow.com/questions/29729049/use-pugixml-to-rename-nodes-based-on-a-stdmap