您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

libxml2实现xml文档的节点的修改

2025/12/13 5:38:32发布43次查看
其实在网上很容易能找到使用libxml2来对xml文档进行创建、解析、修改等。我着这里主要是就自己学习的libxml2修改xml文档的节点进行一个简单的总结,方便自己以后回顾。 下面给出我写的一个例子: /*********************************************************
 其实在网上很容易能找到使用libxml2来对xml文档进行创建、解析、修改等。我着这里主要是就自己学习的libxml2修改xml文档的节点进行一个简单的总结,方便自己以后回顾。 
下面给出我写的一个例子:
/********************************************************************** copyright, 2011, **** tech. co., ltd. all rights reserved ----------------------------------------------------------------------- project code : wlan file name : modify_node.cpp author : sky_qing description : 使用libxml2修改xml文档的节点function list: ----------------------------------------------------------------------- history: date author modification 2011-12-27 sky_qing created file**********************************************************************/#include #include libxml/parser.h#include libxml/tree.hint main(int argc, char* argv[]){ xmldocptr doc; //定义解析文档指针 xmlnodeptr curnode; //定义节点指针(在各个节点之间移动) char* szdocname = argv[1]; //保存xml文档名,该文档名在运行程序到时候输入。 //例如:编译格式为g++ modify_node.cpp -o modify_node -i /usr/local/include/libxml2/ -l /usr/local/lib -lxml2,生成可执行文件modify_node,运行时:./modify_node log4crc(此处log4crc为要修改的xml文档) printf(........start........\n); doc = xmlreadfile(szdocname, utf-8, xml_parse_recover); //解析文档 if (null == doc) { fprintf(stderr, document not parsed successfully.\n); return -1; } curnode = xmldocgetrootelement(doc); //确定文档根元素 if (null == curnode) { fprintf(stderr, empty document.\n); xmlfreedoc(doc); //释放文件 return -1; } if (xmlstrcmp(curnode->name, (const xmlchar*)log4c)) //确认根元素是否为“log4c” { fprintf(stderr, document of wrong type. root node != log4c); xmlfreedoc(doc); return -1; } curnode = curnode->xmlchildrennode; xmlnodeptr propnode = curnode; while (null != curnode) //遍历所有节点 { //获取名称为category的节点 if (!xmlstrcmp(curnode->name, (const xmlchar*)category)) { //查找带有属性name的节点 if (xmlhasprop(curnode, bad_cast name)) { propnode = curnode; } //查找属性name为wlan_console的节点 xmlattrptr attrptr = propnode->properties; while (null != attrptr) //遍历所有名称为category的节点 { if (!xmlstrcmp(attrptr->name, (const xmlchar*)name)) //找到有name属性到节点 { //查找属性为name的值的节点 xmlchar* szpropity = xmlgetprop(propnode, (const xmlchar*)name); if (!xmlstrcmp((const xmlchar*)szpropity, (const xmlchar*)wlan_console)) { xmlattrptr setattrptr = propnode->properties; while (null != setattrptr) { //设置属性priority的值 xmlsetprop(propnode, (const xmlchar*)priority, (const xmlchar*)debug); setattrptr = setattrptr->next; } } } attrptr = attrptr->next; } } curnode = curnode->next; } //保存文档到原文档中 xmlsavefile(log4crc, doc); printf(...........ok............\n); return 0;}
编译和运行格式在代码中有。
参考文章:
http://blog.sina.com.cn/s/blog_669e9f6a0100sbql.html
http://wenku.baidu.com/view/02d0fdea172ded630b1cb61c.html
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product