【问题标题】:Map node names using pugixml for different inputs使用 pugixml 为不同的输入映射节点名称
【发布时间】:2015-06-25 17:39:27
【问题描述】:

问题

我的程序使用 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 以相同的格式(id em> 和 描述)

似乎答案基于此代码:

  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

【问题讨论】:

  • 您是否想知道std::map 的工作原理?
  • @Galik 我真的不知道如何解决这个问题。每个文件都有与相同数据相关的不同节点标签,所以我需要定义映射,以便知道什么数据是什么。

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


【解决方案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());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2012-12-29
    相关资源
    最近更新 更多