私はC++を初めて使用しますが、ノード名の標準セットを定義してからそれらにマップしようとしています。
たとえば、私の標準的なインポート/出力スキーマは次のとおりです。
<data>
<entry>
<id>1</id>
<description>Test</description>
</entry>
</data>
ただし、XMLインポートの名前が異なる場合があるため、マップを作成して、入力ファイルに次の命名規則がある場合でも、上記の形式で出力する必要があります。
<data>
<entry>
<id>1</id>
<content>Test</content>
</entry>
</data>
このコードは、ドキュメントと私が得たヘルプに基づいた私の最善の推測ですが、完成させるのに行き詰まりました:
#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());
}
}
}
このコードは、理想的には xml をどちらの方法でもフォーマットできるようにしますが、出力は常に同じになります。どんな助けでも本当にありがたいです。上記のコードでは、次のエラーが表示されます。
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());
^