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>

考えられる解決策

非標準のマッピング (ノードの名前) を定義して、入力ファイルのノードの名前に関係なく、常に同じ形式 ( iddescription )で std:cout することは可能ですか?

答えはこのコードに基づいているようです:

  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 に答える