XML 解析程序没有可行的重载

No viable overloaded on XML parsing program

我有以下代码比较 XML 文件,然后使用映射根据节点名称定义内容类型,并使用 if 命令尝试根据标签是什么采取行动找到了。

#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 (auto& eb: mapb) {
         for (auto& kv : tagMap) {
         kv.first = eb.second.child_value(kv.second.c_str());
             if (kv.first == "id") {
             // Do work on id 
             }
             if (kv.first == "description") {
             // Do work on description 
             }
             if (kv.first == "url") {
             // Do work on URL data (I.e validate it)
             }
             if (kv.first == "location") {
             // Do work on location data
             }
         }
     }
}

我遇到的问题是当我尝试编译这个程序时出现这个错误:

g++ -g -Wall -std=c++11 -I include -o main src/main.cpp include/pugi/pugixml.cpp 
src/main.cpp:46:19: error: no viable overloaded '='
         kv.first = eb.second.child_value(kv.second.c_str());

这是一个示例输入:

<data>
<entry>
<id>1</id>
<content>Test</content>
<web_address>test.com</web_address>
</entry>
</data>

您无法覆盖 std::map 等关联容器中的密钥——它总是会作为 const 参考返回给您。实际上,如果您被允许覆盖密钥,那么您很容易违反容器的不变量。通常的解决方案是删除该元素并使用更新后的键插入一个新元素。

我可能在这里偏离基地,但根据你的描述,我认为你可能正在寻找这样的东西。

我怀疑你的标签映射是倒退的,但我无法在你的示例数据上使用它,所以可能还缺少其他东西。

希望我所做的可能有用。

int main()
{

    // I think maybe your map is backwards?
    const std::map<std::string, std::string> tagMap {
//        {"description", "content"}, {"url", "web_address"}
        {"content", "description"}, {"web_address", "url"}
    };

    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")) {
    std::string id = node.child_value("id");
    mapa[id] = node;
    }

    for (auto& node: docb.child("data").children("entry")) {
    std::string idcs = node.child_value("id");

        if (!mapa.erase(idcs)) {
        mapb[idcs] = node;
        }
    }

    // Use this to find mapped nodes
    std::map<std::string, std::string>::const_iterator found;

     for (auto& eb: mapb) {

             // try to find the node in the map
             found = tagMap.find(eb.second.child_value());

             if(found == tagMap.end()) // - FAIL
                 continue; // unknown tag - ignore

             // test the corresponding (mapped) tag name

            if (found->second == "id") {
                 std::cout << "// Do work on id" << '\n';
             }
             if (found->second == "description") {
                 std::cout << "// Do work on description" << '\n';
             }
             if (found->second == "url") {
                 std::cout << "// Do work on URL data (I.e validate it)" << '\n';
             }
             if (found->second == "location") {
                 std::cout << "// Do work on location data" << '\n';
             }
//         }
     }
}