gpt4 book ai didi

c++ - 使用 pugixml 为不同的输入映射节点名称

转载 作者:行者123 更新时间:2023-11-28 06:18:43 25 4
gpt4 key购买 nike

问题

我的程序使用 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>

可能的解决方案

是否可以定义非标准映射(节点名称),以便无论输入文件中的节点名称是什么,我总是以相同的格式(id描述)

看起来答案是基于这段代码:

  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

最佳答案

也许您正在寻找这样的遗嘱?

#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());
}
}

关于c++ - 使用 pugixml 为不同的输入映射节点名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29723443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com