【问题标题】:No viable overloaded on XML parsing programXML解析程序没有可行的重载
【发布时间】:2015-06-27 17:48:00
【问题描述】:

我有以下代码比较 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>

【问题讨论】:

  • 您正在尝试更改地图的关键元素。你为什么要改变这些?你能不能只将eb.second.child_value(kv.second.c_str()) 分配给它自己的变量并使用它进行if() 测试?
  • @Galik 我可以,是的,但是我对如何将其与映射相关联感到困惑。例如,如果它读取content 标签,我想将它放在is a description 的类别中,因为我的地图将内容标签定义为描述。
  • @Galik 这意味着&lt;content&gt;Test&lt;/content&gt; 将属于if x == "description") {,所以我可以在“测试”测试中工作
  • 我没看到。您将标记名称映射到其规范化值,然后检查该映射名称。我认为您只需要使用单独的变量来临时存储映射名称而不是输入名称。
  • 我不完全确定您要做什么,但我想的更像是:std::string var = eb.second.child_value(kv.second.c_str()); if(var == "id") {} else if(var == "url") {} etc... 但也许我错过了什么?

标签: c++ xml c++11 xml-parsing


【解决方案1】:

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

【讨论】:

  • 所以问题出在这一行? kv.first = eb.second.child_value(kv.second.c_str());。我需要使用地图检查它是什么类型的信息,即&lt;content&gt;Test&lt;/content&gt; 是描述。根据你所说的,你建议我如何解决这个问题?
  • @Jimmy 您正试图覆盖地图tagMap 中的键,我告诉您这是无法做到的。如果您需要覆盖这些值,那很好,您可以分配给kv.second
【解决方案2】:

我可能不在此处,但根据您的描述,我认为您可能正在寻找类似的东西。

我怀疑您的标签映射是向后的,但我无法在您的示例数据上进行此操作,因此可能还缺少其他内容。

希望我所做的可能会有一些用处。

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';
             }
//         }
     }
}

【讨论】:

  • 你能告诉我如何打印出 if 标签内的标签内容吗?
猜你喜欢
  • 2012-08-03
  • 2015-06-26
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多