【发布时间】:2015-06-26 01:35:58
【问题描述】:
我是 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());
^
【问题讨论】:
-
当你说“试图完成它时遇到了困难”,你会遇到什么错误,或者与它应该做的相比,它做了什么?
-
@Kvothe 添加了为什么它不起作用,错误
标签: c++ xml c++11 xml-parsing pugixml