0

问题

我的程序使用pugixml从文件中吐出 XML 节点。这是执行此操作的代码位:

for (auto& ea: mapa) {
    std::cout << "Removed:" << std::endl;
    ea.second.print(std::cout);
}

for (auto& eb: mapb) {
    std::cout << "Added:" << std::endl;
    eb.second.print(std::cout);
}

吐出的所有节点都应具有以下格式(例如 filea.xml):

<entry>
    <id><![CDATA[9]]></id>
    <description><![CDATA[Dolce 27 Speed]]></description>
 </entry>

然而,吐出的内容取决于输入数据的格式。有时标签被称为不同的东西,我最终可能会这样(例如 fileb.xml):

<entry>
    <id><![CDATA[9]]></id>
    <mycontent><![CDATA[Dolce 27 Speed]]></mycontent>
 </entry>

可能的解决方案

是否可以定义非标准映射(节点名称),以便无论输入文件上的节点名称是什么,我总是 std:cout 以相同的格式(iddescription

似乎答案基于以下代码:

  description = mycontent; // Define any non-standard maps
  std::cout << node.set_name("notnode");
  std::cout << ", new node name: " << node.name() << std::endl;

我是 C++ 新手,因此对如何实现这一点的任何建议将不胜感激。我必须在数以万计的字段上运行它,所以性能是关键。

参考

https://pugixml.googlecode.com/svn/tags/latest/docs/manual/modify.html https://pugixml.googlecode.com/svn/tags/latest/docs/samples/modify_base.cpp

4

1 回答 1

1

也许像这样的东西就是你要找的东西?

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

#include "pugixml.hpp"

using namespace pugi;

int main()
{
    // tag mappings
    const std::map<std::string, std::string> tagmaps
    {
          {"odd-id-tag1", "id"}
        , {"odd-id-tag2", "id"}
        , {"odd-desc-tag1", "description"}
        , {"odd-desc-tag2", "description"}
    };

    // working registers
    std::map<std::string, std::string>::const_iterator found;

    // loop through the nodes n here
    for(auto&& n: nodes)
    {
        // change node name if mapping found
        if((found = tagmaps.find(n.name())) != tagmaps.end())
            n.set_name(found->second.c_str());
    }
}
于 2015-04-18T23:01:32.560 回答