2

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());
                       ^
J.Zil
  • 2,397
  • 7
  • 44
  • 78
  • possible duplicate of [Map node names using pugixml for different inputs](http://stackoverflow.com/questions/29723443/map-node-names-using-pugixml-for-different-inputs) – m.s. Apr 19 '15 at 11:18
  • 1
    When you say "got stuck trying to complete it" what errors do you get or what does it do compared to what it should do? – Kvothe Apr 19 '15 at 11:50
  • @Kvothe Added why it doesnt work, the errors – J.Zil Apr 19 '15 at 12:27
  • possible duplicate of [No viable overloaded '=' on a c++ program](http://stackoverflow.com/questions/29730796/no-viable-overloaded-on-a-c-program) – Kvothe Apr 19 '15 at 15:32

1 Answers1

0

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.

Kvothe
  • 1,819
  • 2
  • 23
  • 37
  • Thank you for the reply. In this case I get this error ```src/main.cpp:49:19: error: no viable overloaded '=' if((found = tagMap.find(n.name())) != tagMap.end()) {```, do you know what might be causing this? – J.Zil Apr 19 '15 at 12:58
  • 1
    See my edit, I think that should solve that error. `found` needs to be set before the `if` condition – Kvothe Apr 19 '15 at 13:04
  • 2
    @Kvothe No, you cannot assign a `map::const_iterator` to a `xml_node`. – Barry Apr 19 '15 at 14:23
  • @Barry Oh yes, just looked at it again now - I'll amend it later (I have to get back to work now), or amend it yourself if you have time now – Kvothe Apr 19 '15 at 14:26