转自:http://blog.csdn.net/sky_qing/article/details/7165010
http://blog.chinaunix.net/uid-20680966-id-3475787.html
======================================================================
其实在网上很容易能找到使用libxml2来对xml文档进行创建、解析、修改等。我着这里主要是就自己学习的libxml2修改xml文档的节点进行一个简单的总结,方便自己以后回顾。
下面给出我写的一个例子:
-
- Copyright,2011,****Tech.Co.,Ltd.
- AllRightsReserved
- -----------------------------------------------------------------------
- ProjectCode:wlan
- Filename:modify_node.cpp
- Author:Sky_qing
- Description:使用libxml2修改xml文档的节点
- FunctionList:
- History:
- DateAuthorModification
- 2011-12-27Sky_qingcreatedfile
- **********************************************************************/
- #include<stdio.h>
- #include"libxml/parser.h"
- #include"libxml/tree.h"
- intmain(intargc,char*argv[])
- {
- xmlDocPtrdoc;
- xmlNodePtrcurNode;
- char*szDocName=argv[1];
- //例如:编译格式为g++modify_node.cpp-omodify_node-I/usr/local/include/libxml2/-L/usr/local/lib-lxml2,生成可执行文件modify_node,运行时:./modify_nodelog4crc(此处log4crc为要修改的xml文档)
- printf("........start........n");
- doc=xmlReadFile(szDocName,"utf-8",XML_PARSE_RECOVER);
- if(NULL==doc)
- {
- fprintf(stderr,0); background-color:inherit">"Documentnotparsedsuccessfully.n");
- return-1;
- }
- curNode=xmlDocGetRootElement(doc);
- if(NULL==curNode)
- "EmptyDocument.n");
- xmlFreeDoc(doc);
- return-1;
- }
- if(xmlStrcmp(curNode->name,(constxmlChar*)"log4c"))
- "Documentofwrongtype.rootnode!=log4c");
- xmlFreeDoc(doc);
- curNode=curNode->xmlChildrenNode;
- xmlNodePtrpropNode=curNode;
- while(NULL!=curNode)
- //获取名称为category的节点
- if(!xmlStrcmp(curNode->name,0); background-color:inherit">"category"))
- //查找带有属性name的节点
- if(xmlHasProp(curNode,BAD_CAST"name"))
- propNode=curNode;
- //查找属性name为WLAN_Console的节点
- xmlAttrPtrattrPtr=propNode->properties;
- while(NULL!=attrPtr)
- if(!xmlStrcmp(attrPtr->name,0); background-color:inherit">"name"))
- //查找属性为name的值的节点
- xmlChar*szPropity=xmlGetProp(propNode,("name");
- if(!xmlStrcmp((constxmlChar*)szPropity,0); background-color:inherit">"WLAN_Console"))
- xmlAttrPtrsetAttrPtr=propNode->properties;
- while(NULL!=setAttrPtr)
- //设置属性priority的值
- xmlSetProp(propNode,0); background-color:inherit">"priority",0); background-color:inherit">"debug");
- setAttrPtr=setAttrPtr->next;
- attrPtr=attrPtr->next;
- curNode=curNode->next;
- //保存文档到原文档中
- xmlSaveFile("log4crc",doc);
- "...........OK............n");
- return0;
- }
- xmlversion="1.0"encoding="UTF-8"?>
- <radios>
- radio>
- name>Bayernname>
- url>http://mp3.webradio.antenne.de:80url>
- classificationarea>usaarea>
- style>musicstyle>
- classification>
- radio>
- >DEU-AntenneBayern>http://testradios>
上代码
staticxmlXPathObjectPtrgetNodeset(xmlDocPtrdoc,constxmlChar*xpath)
- {
- xmlXPathContextPtrcontext;
- xmlXPathObjectPtrresult;
- context=xmlXPathNewContext(doc);
-
- if(context==NULL){
- printf("contextisNULLn");
- returnNULL;
- }
-
- result=xmlXPathEvalExpression(xpath,context);
- xmlXPathFreeContext(context);
- if(result==NULL){
- printf("xmlXPathEvalExpressionreturnNULLn");
- returnNULL;
- }
- if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
- xmlXPathFreeObject(result);
- printf("nodesetisemptyn");
- returnresult;
- }
playlistDoc 为 xmlDocPtr类型.
xmlChar*xpath=BAD_CAST("/radios/radio[name='DEU-AntenneBayern']");
- xmlXPathObjectPtrapp_result=getNodeset(playlistDoc,xpath);
- if(app_result==NULL)
- printf("app_resultisNULLn");
- return;
- inti=0;
- xmlChar*value;
- if(app_result)
- xmlNodeSetPtrnodeset=app_result->nodesetval;
- xmlNodePtrcur;
- for(i=0;i<nodeset->nodeNr;i++)
- {
- cur=nodeset->nodeTab[i];
- cur=cur->xmlChildrenNode;
- while(cur!=NULL)
- if(!xmlStrcmp(cur->name,(constxmlChar*)"name"))
- printf("%sn",((char*)XML_GET_CONTENT(cur->xmlChildrenNode)));
- elseconstxmlChar*)"url"))
- cur=cur->next;
- xmlXPathFreeObject(app_result);
- }
输出:
DEU-Antenne Bayern http://mp3.webradio.antenne.de:80 DEU-Antenne Bayern http://test
xmlChar*xpath=BAD_CAST("/radios/radio[name='DEU-AntenneBayern']");
改成
copy
xmlChar*xpath=BAD_CAST("/radios/radio[name='DEU-AntenneBayern'andurl='http://mp3.webradio.antenne.de:80']");
- EU-AntenneBayern
输出:
http://mp3.webradio.antenne.de:80 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|