2

私は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());
                       ^
4

1 に答える 1

0

変数foundnは宣言されません。コードのセクションが次のようになるように、ループの前にこれらの変数を適切な型として宣言します。

編集:コードを少し変更しました。if ステートメントは、設定後に found の値をチェックする必要があります。

pugi::xml_node found, n;

for (auto& eb: mapb) {
    // change node name if mapping found
    found = tagmaps.find(n.name());
    if((found != tagmaps.end()) {
        n.set_name(found->second.c_str());
    }
}

また、nループ内の特定のノードに設定する必要があると思います(現時点では値がありません)。nこの変数が何を保持する必要があるかを明確にするために、別の名前に変更することを検討してください。

于 2015-04-19T12:46:14.477 に答える