pugixml的官网 https://pugixml.org/ 在github上下载例程中有使用手册
<?xml version="1.0"?>
<Students>
<Student id="2018" name="chen" />
<cyc len="100" />
</Students>
1、打开xml文件
pugi::xml_document doc ;
doc.load_file("map.xml");
2、添加申明
pugi::xml_document doc ;
pugi::xml_node pre = doc.prepend_child(pugi::node_declaration);
pre.append_attribute("version") = "1.0";
pre.append_attribute("encoding") = "utf-8";
doc.save_file("./map.xml")
3、添加节点及属性
pugi::xml_document doc ;
doc.load_file("map.xml");
pugi::xml_node xstudents = doc.append_child("Students");
xstudents.append_child(pugi::node_comment).set_value("Students!");
pugi::xml_node xstudent = xstudents.append_child("Student");
pugi::xml_attribute xstudent_id = xstudent.append_attribute("id");
xstudent_id.set_value("2018");
pugi::xml_attribute xstudent_name = xstudent.append_attribute("name");
xstudent_name.set_value("chen");
xstudents.append_child("cyc");
doc.child("Students").child("cyc").append_attribute("len").set_value("100");
doc.save_file("./map.xml")
4、移动
doc.load_file("./map.xml");
doc.child("Students").append_move(doc.child("Students").child("cyc"));
doc.child("Students").prepend_move(doc.child("Students").child("cyc"));
doc.child("Students").insert_move_before(doc.child("Students").child("cyc").next_sibling(),doc.child("Students").child("cyc"));
5、读取:
pugi::xml_node infos = doc.child("Students");
for(pugi::xml_node info = infos.first_child() ; info; info = info.next_sibling())
{
for(pugi::xml_attribute attr = info.first_attribute(); attr; attr = attr.next_attribute())
{
qDebug()<<info.name()<<attr.name()<<attr.value();
if(QString(info.name()) == "cyc")
{
qDebug()<<"@@@@";
}
}
}
6、上下节点
doc.child("Students").child("cyc").next_sibling()//下一个节点
doc.child("Students").child("cyc").before_sibling()//上一个节点