pugixml

来源:互联网 发布:椒江淘宝摄影 编辑:程序博客网 时间:2024/05/12 14:03
使用pugixml库需要三个文件:pugiconfig.h/pugixml.h/pugixml.cpp,可直接从gugixml官网下载,将其加入工程,使用处包含头文件pugiconfig.h/pugixml.h即可。
#include <iostream>#include "pugiconfig.hpp"#include "pugixml.hpp"#include <string.h>#include <stdio.h>#include <stdlib.h>using namespace std;using namespace pugi;//读节点属性bool ReadXml(const char *xmlfile){xml_document doc;if(doc.load_file(xmlfile)){cout<<"....."<<endl;return false;}//input属性值xml_node form=doc.child("root").child("form");//得到根节点下的子节点string ip=form.attribute("ip").value();//得到属性值string port=form.attribute("port").value();//得到属性值string action=form.attribute("action").value();//得到属性值cout<<ip.data()<<endl;cout<<port.data()<<endl;cout<<action.data()<<endl;return true;}//遍历根节点下的字节点bool TravelXml(const char *xmlfile){xml_document doc;if(doc.load_file(xmlfile)){cout<<"load error"<<endl;return false;}xml_node form=doc.child("root").child("form");for(xml_node input=form.first_child();input;input=input.next_sibling()){string strname=input.attribute("name").value();cout<<"name="<<strname<<endl;if(!strname.empty()){string strvalue=input.attribute("value").value();cout<<"value="<<strvalue<<endl;}}return true;}//增加节点bool AppendXml(const char *xmlfile){xml_document doc;if(!doc.load_file(xmlfile)){cout<<"load error"<<endl;return false;}xml_node root=doc.child("root");root.append_child("wei");doc.save_file(xmlfile);return true;}//删除节点bool RemoveXml(const char *xmlfile){xml_document doc;if(!doc.load_file(xmlfile)){cout<<"load error"<<endl;return false;}xml_node root=doc.child("root");root.remove_child("wei");doc.save_file(xmlfile);return true;}int main(void){ReadXml("./example1.xml");//TravelXml("./example1.xml");//AppendXml("./example1.xml");//RemoveXml("./example1.xml");return 0;}

0 0
原创粉丝点击