在返回的XML上循环映射以获取节点值 在我的C++程序中,我从XML文件中吐出节点。我有一个标准模式,输入文件可能不遵循该模式。因此,我需要用包含在其中的信息类型映射节点标题 #include "pugi/pugixml.hpp" #include <iostream> #include <string> #include <map> int main() { const std::map<std::string, std::string> tagMap { {"description", "content"}, {"url", "web_address"} }; pugi::xml_document doca, docb; std::map<std::string, pugi::xml_node> mapa, mapb; if (!doca.load_file("a.xml") || !docb.load_file("b.xml")) { std::cout << "Can't find input files"; return 1; } 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 removed for (auto& ea: mapa) { std::cout << "Removed:" << std::endl; ea.second.print(std::cout); } // For added for (auto& eb: mapb) { // Loop through tag map for (auto& kv : tagMap) { // Try to find the tag name named in second map value // and associate it to the type of information in first map value std::cout << "Found" << kv.first; std::cout << "which has value" << node.child_value(kv.second) } } }
错误消息非常清楚:您没有在输出代码的范围内定义在返回的XML上循环映射以获取节点值 在我的C++程序中,我从XML文件中吐出节点。我有一个标准模式,输入文件可能不遵循该模式。因此,我需要用包含在其中的信息类型映射节点标题 #include "pugi/pugixml.hpp" #include <iostream> #include <string> #include <map> int main() { const std::map<std::string, std::string> tagMap { {"description", "content"}, {"url", "web_address"} }; pugi::xml_document doca, docb; std::map<std::string, pugi::xml_node> mapa, mapb; if (!doca.load_file("a.xml") || !docb.load_file("b.xml")) { std::cout << "Can't find input files"; return 1; } 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 removed for (auto& ea: mapa) { std::cout << "Removed:" << std::endl; ea.second.print(std::cout); } // For added for (auto& eb: mapb) { // Loop through tag map for (auto& kv : tagMap) { // Try to find the tag name named in second map value // and associate it to the type of information in first map value std::cout << "Found" << kv.first; std::cout << "which has value" << node.child_value(kv.second) } } },c++,xml,parsing,c++11,pugixml,C++,Xml,Parsing,C++11,Pugixml,错误消息非常清楚:您没有在输出代码的范围内定义节点 在for(auto&node:…)中定义节点时,它仅在for循环的范围内可见 我不完全清楚它应该是什么,但我想你应该用类似于eb.second.child\u值(kv.second)的东西来替换node.child\u值(kv.second)是我应该在那个循环中定义节点,还是当我应该使用其他东西时尝试使用node是错误的?例如,它应该是kv.child_值吗?@JamesWillson我认为你应该使用其他值kv.second.child\u值(
节点
在for(auto&node:…)
中定义节点时,它仅在for
循环的范围内可见
我不完全清楚它应该是什么,但我想你应该用类似于eb.second.child\u值(kv.second)
的东西来替换node.child\u值(kv.second)
是我应该在那个循环中定义节点,还是当我应该使用其他东西时尝试使用node是错误的?例如,它应该是kv.child_值吗?@JamesWillson我认为你应该使用其他值kv.second.child\u值(…)
可能是一个很好的候选者<代码>eb.second…
另一个(因为其他原因,您将在mapb中循环,而不使用其元素…)。
<content>Hello!</content>
<web_address>www.hotmail.com</web_address>