我正在寻找一种优雅的解决方案,以使用pugixml(1.6版)替换节点pcdata。例如,遍历节点集并将子值更新为某种值。
pugi::xpath_node_set nodes = document.select_nodes("//a");
for (auto it = nodes.begin(); it != nodes.end(); it++)
{
std::cout << "before : " << it->node().child_value() << std::endl;
// SOME REPLACE GOES HERE
std::cout << "after : " << it->node().child_value() << std::endl;
}
我使用了:
it->node().append_child(pugi::node_pcdata).set_value("foo");
但是顾名思义,它只是追加了数据,但我找不到类似于以下内容的任何函数:
it->node().remove_child(pugi::node_pcdata);
另一个注意事项是节点上的属性很重要,应保持不变。
谢谢你的帮助。
最佳答案
xml_text对象是用于此目的(以及其他目的):
std::cout << "before : " << it->node().child_value() << std::endl;
it->node().text().set("contents");
std::cout << "after : " << it->node().child_value() << std::endl;
请注意,您还可以使用text()代替child_value(),例如:
xml_text text = it->node().text();
std::cout << "before : " << text.get() << std::endl;
text.set("contents");
std::cout << "after : " << text.get() << std::endl;
此页面具有更多详细信息:http://pugixml.org/docs/manual.html#access.text
关于c++ - 如何使用pugixml替换节点pcdata或文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30789222/