C++ 将变量传递给PUGIXML xml_tree_walker

C++ 将变量传递给PUGIXML xml_tree_walker,c++,pugixml,C++,Pugixml,我正在使用pugixml解析一个文件。我一直在使用一个xml_tree_walker,当walker遍历xml时,我想修改一个变量。我目前正在使用一个全局变量,但不想使用。有没有办法通过引用将变量传递给walker?如果是这种情况,这是否意味着我需要修改pugixml源中每个函数的 下面是我目前使用助行器的方式 struct simple_walker: pugi::xml_tree_walker { virtual bool for_each(pugi::xml_node& n

我正在使用pugixml解析一个文件。我一直在使用一个xml_tree_walker,当walker遍历xml时,我想修改一个变量。我目前正在使用一个全局变量,但不想使用。有没有办法通过引用将变量传递给walker?如果是这种情况,这是否意味着我需要修改pugixml源中每个函数的

下面是我目前使用助行器的方式

struct simple_walker: pugi::xml_tree_walker
{
    virtual bool for_each(pugi::xml_node& node)
    {
        for (int i = 0; i < depth(); ++i) std::cout << "  "; // indentation

        std::cout << node_types[node.type()] << ": name='" << node.name() << "', value='" << node.value() << "'\n";

        some_global = node.name(); // I don't want this to be a global

        return true; // continue traversal
    }
};

您可以做的一件事是在walker中保留一个成员引用,指向您正在捕获信息的对象。比如:

struct captured_info // where my captured info goes
{
    std::string name;
    int value;
    std::time_t date;
    // ...
};

struct simple_walker: pugi::xml_tree_walker
{
    // must supply a captured_info object to satisfy
    // this constructor
    simple_walker(captured_info& info): info(info) {}

    virtual bool for_each(pugi::xml_node& node)
    {
        for (int i = 0; i < depth(); ++i) std::cout << "  "; // indentation

        std::cout << node_types[node.type()] << ": name='" << node.name() << "', value='" << node.value() << "'\n";

        info.name = node.name(); 
        info.value = std::stoi(node.value()); 
        // ... etc

        return true; // continue traversal
    }

    captured_info& info;
};


pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(XML_FILE);

captured_info info; // the output!!!
simple_walker mySimpleWalker(info); // configure for output
doc.traverse(mySimpleWalker);

std::cout << info.name << '\n'; // etc...
struct captured\u info//我捕获的信息在哪里
{
std::字符串名;
int值;
标准:时间和日期;
// ...
};
struct simple_walker:pugi::xml_tree_walker
{
//必须提供捕获的_info对象才能满足
//这个构造函数
简单步行者(捕获的信息和信息):信息(信息){
每个节点的虚拟bool(pugi::xml\u节点和节点)
{

对于(int i=0;isome_global
作为
simple_walker
的成员变量?如何创建它并调用
pugi
API?@Galik我在问题中添加了如何创建和调用它。该变量实际上是另一个结构,walker将使用该结构修改其中的一些成员。此结构将由其他函数使用/修改,因此我不确定它作为
simple\u walker
struct?的成员变量是否有意义。将结构作为成员不是问题。但这取决于您是否需要将该结构放在其他地方(或谁需要拥有它).如果不知道整个情况,这是很困难的,但是如果不是一个成员变量作为一个值,那么作为一个引用的成员变量呢?
struct captured_info // where my captured info goes
{
    std::string name;
    int value;
    std::time_t date;
    // ...
};

struct simple_walker: pugi::xml_tree_walker
{
    // must supply a captured_info object to satisfy
    // this constructor
    simple_walker(captured_info& info): info(info) {}

    virtual bool for_each(pugi::xml_node& node)
    {
        for (int i = 0; i < depth(); ++i) std::cout << "  "; // indentation

        std::cout << node_types[node.type()] << ": name='" << node.name() << "', value='" << node.value() << "'\n";

        info.name = node.name(); 
        info.value = std::stoi(node.value()); 
        // ... etc

        return true; // continue traversal
    }

    captured_info& info;
};


pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(XML_FILE);

captured_info info; // the output!!!
simple_walker mySimpleWalker(info); // configure for output
doc.traverse(mySimpleWalker);

std::cout << info.name << '\n'; // etc...