使用pugixml为不同的输入映射节点名称 [英] Map node names using pugixml for different inputs
问题描述
我的程序使用 pugixml 从文件中输出XML节点。这是这样做的代码位:
My program spits out XML nodes from a file using pugixml. This is the bit of the code which does this:
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):
All nodes spat out should have this format (for example filea.xml):
<entry>
<id><![CDATA[9]]></id>
<description><![CDATA[Dolce 27 Speed]]></description>
</entry>
但是什么是输出取决于输入数据的格式。有时标签被称为不同的东西,我可以结束这个(例如fileb.xml):
However what is spat out depends on how the input data is formatted. Sometimes the tags are called different things and I could end up with this (for example fileb.xml):
<entry>
<id><![CDATA[9]]></id>
<mycontent><![CDATA[Dolce 27 Speed]]></mycontent>
</entry>
可能的解决方案
定义非标准映射(节点名称),这样,无论输入文件上的节点名称是什么,我总是std:cout它以相同的格式( id 和 description
Possible solution
Is it possible to define non standard mappings (names of nodes) so that, no matter what the names of the nodes are on the input file, I always std:cout it in the same format (id and description)
似乎答案是基于以下代码:
It seems like the answer is based on this code:
description = mycontent; // Define any non-standard maps
std::cout << node.set_name("notnode");
std::cout << ", new node name: " << node.name() << std::endl;
我是C ++的新手,所以任何建议如何实现这将不胜感激。我必须在数万个字段上运行此操作,所以性能是关键。
I'm new to C++ so any suggestions on how to implement this would be appreciated. I have to run this on tens of thousands of fields so performance is key.
https://pugixml.googlecode.com/svn/tags/latest/docs/ manual / modify.html
https: //pugixml.googlecode.com/svn/tags/latest/docs/samples/modify_base.cpp
推荐答案
也许这样的东西是你要找的?
Maybe something like this will is what you're looking for?
#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());
}
}
这篇关于使用pugixml为不同的输入映射节点名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!