bool ModifyNode()
{
pugi::xml_document doc;
if (!doc.load_buffer("",0))
{
std::cout<<"nothing Loading!"<<std::endl;
return false;
}
//添加一个根节点
pugi::xml_node rootNode = doc.append_child("root");
//添加一个节点
pugi::xml_node ChildNode = rootNode.append_child("test");
//然后显示当前的xml
std::cout<<"/// 修改之前的xml ///"<<std::endl;
doc.print(std::cout);
std::cout<<"/// 修改之后的xml ///"<<std::endl;
ChildNode.set_name("SafeDebug");
doc.print(std::cout);
std::cout<<"/// 设置属性值后的XML ///"<<std::endl;
//添加属性
pugi::xml_attribute attr = ChildNode.append_attribute("age");
//添加属性值
attr.set_value("23");
//设置节点文本
ChildNode.append_child(pugi::node_pcdata).set_value("Simple node");
doc.print(std::cout);
return true;
}
官方演示的属性操作
pugi::xml_document doc;
//[code_modify_add
// add node with some name
pugi::xml_node node = doc.append_child("node");
// add description node with text child
pugi::xml_node descr = node.append_child("description");
descr.append_child(pugi::node_pcdata).set_value("Simple node");
// add param node before the description
pugi::xml_node param = node.insert_child_before("param", descr);
// add attributes to param node
param.append_attribute("name") = "version";
param.append_attribute("value") = 1.1;
param.insert_attribute_after("type", param.attribute("name")) = "float";
//]