1

In my C++ program I spit out nodes from an XML file. I have a standard schema which may not be followed by the input file. I therefore need to map a node title with the information type which is contained within it.

#include "pugi/pugixml.hpp"

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

int main()
{

    const std::map<std::string, std::string> tagMap {
        {"description", "content"}, {"url", "web_address"}
    };

    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("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 removed
    for (auto& ea: mapa) {
    std::cout << "Removed:" << std::endl;
    ea.second.print(std::cout);
    }

    // For added
    for (auto& eb: mapb) {
        // Loop through tag map
        for (auto& kv : tagMap) {
            // Try to find the tag name named in second map value
            // and associate it to the type of information in first map value
            std::cout << "Found" << kv.first;
            std::cout << "which has value" << node.child_value(kv.second)
        }
    }

}

The information I am particualy asking for help with is within for (auto& eb: mapb) {. Here I am trying to look at the XML recevied and see if I can match the tags to names in the map (i.e content and web_address) and if so, print the value of the node, associating it to what is it (i.e description or url).

I haven't been able to test this because of this compilation error, which I don't understand because I have refered to node above:

g++ -g -Wall -std=c++11 -I include -o main src/main.cpp include/pugi/pugixml.cpp src/main.cpp:51:38: error: use of undeclared identifier 'node' std::cout << "which has value" << node.child_value(kv.second)

My expected output is this:

  • Found description which has this value Hello!
  • Found url which has this value www.hotmail.com

From this input

<content>Hello!</content>
<web_address>www.hotmail.com</web_address> 

1 Answer 1

2

The error message is pretty clear: you didn't define node in the scope of your output code.

When you defined the node in for (auto& node: ...) , it's visible only in the scope of the for loop.

It's not totally clear for me what it should be but I guess you should replace node.child_value(kv.second) with something like eb.second.child_value(kv.second)

2
  • Is it a case that I should be defining node in that loop, or am I instead incorrect by trying to use node when I should be using something else? Should it be kv.child_value for example ?
    – J.Zil
    Apr 19, 2015 at 18:58
  • @JamesWillson I think you should use something else. kv.second.child_value (...) could be a good candidate. eb.second... another one (because otherwhse you would loop through mapb without doing anything with its elements...).
    – Christophe
    Apr 19, 2015 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.