使用pugiXML根据std :: map重命名节点 [英] Use pugiXML to rename nodes based on a std::map

查看:176
本文介绍了使用pugiXML根据std :: map重命名节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,但我想定义一组标准的节点名称,然后映射到它们。



例如,我的标准导入/输出模式是:

  < data> 
< entry>
< id> 1< / id>
< description>测试< / description>
< / entry>
< / data>但是有时候我的XML导入将会有不同的名称,所以我想创建一个地图,所以它仍然输出。即使输入文件具有此命名约定,也是上述格式:

 < data> 
< entry>
< id> 1< / id>
< content> Test< / content>
< / entry>
< / data>

这段代码是我最好的猜测,基于文档和帮助,试图完成它:

  #includepugi / pugixml.hpp

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

int main()
{

//定义映射,默认左 - 映射在右边
const std :: map< std :: string,std :: string> tagmaps
{
{id,id}
,{description,content}
};

pugi :: xml_document doca,docb;
std :: map< std :: string,pugi :: xml_node>马塔岛

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){
//如果找到映射更改节点名称
if((found = tagmaps。 find(n.name()))!= tagmaps.end()){
n.set_name(found-> second.c_str());
}

}

}

这段代码理想地允许xml格式化,但总是输出相同。任何帮助将非常感激。上面的代码给我以下错误:

  src / main.cpp:34:13:error:using undeclared identifier'发现'
if((found = tagmaps.find(n.name()))!= tagmaps.end()){
^
src / main.cpp:34:34:错误:使用未声明的标识符'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:使用未声明的标识符'found'
n.set_name(found-> second.c_str
^


解决方案

c> found n 永不声明。在该循环之前声明这些变量为适当的类型,使得代码段如下所示:



EDIT:稍微更改了代码,if语句应该在设置之后检查找到的值。

  pugi :: xml_node found,n; 

for(auto& eb:mapb){
//如果找到映射更改节点名称
found = tagmaps.find(n.name());
if((found!= tagmaps.end()){
n.set_name(found-> second.c_str());
}
}

此外,我认为 n 应设置为特定节点在循环内部(在它没有值的时刻)。考虑重命名 n 到其他东西,以使这个变量应该持有。


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());
                       ^

解决方案

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.

这篇关于使用pugiXML根据std :: map重命名节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆