libxml2库解析xml文档举例

转自:http://blog.csdn.net/sky_qing/article/details/7165010

http://blog.chinaunix.net/uid-20680966-id-3475787.html

======================================================================

其实在网上很容易能找到使用libxml2来对xml文档进行创建、解析、修改等。我着这里主要是就自己学习的libxml2修改xml文档的节点进行一个简单的总结,方便自己以后回顾。

下面给出我写的一个例子:

  1. /**********************************************************************
  2. Copyright,2011,****Tech.Co.,Ltd.
  3. AllRightsReserved
  4. -----------------------------------------------------------------------
  5. ProjectCode:wlan
  6. Filename:modify_node.cpp
  7. Author:Sky_qing
  8. Description:使用libxml2修改xml文档的节点
  9. FunctionList:
  10. History:
  11. DateAuthorModification
  12. 2011-12-27Sky_qingcreatedfile
  13. **********************************************************************/
  14. #include<stdio.h>
  15. #include"libxml/parser.h"
  16. #include"libxml/tree.h"
  17. intmain(intargc,char*argv[])
  18. {
  19. xmlDocPtrdoc;//定义解析文档指针
  20. xmlNodePtrcurNode;//定义节点指针(在各个节点之间移动)
  21. char*szDocName=argv[1];//保存xml文档名,该文档名在运行程序到时候输入。
  22. //例如:编译格式为g++modify_node.cpp-omodify_node-I/usr/local/include/libxml2/-L/usr/local/lib-lxml2,生成可执行文件modify_node,运行时:./modify_nodelog4crc(此处log4crc为要修改的xml文档)
  23. printf("........start........\n");
  24. doc=xmlReadFile(szDocName,"utf-8",XML_PARSE_RECOVER);//解析文档
  25. if(NULL==doc)
  26. {
  27. fprintf(stderr,0); background-color:inherit">"Documentnotparsedsuccessfully.\n");
  28. return-1;
  29. }
  30. curNode=xmlDocGetRootElement(doc);//确定文档根元素
  31. if(NULL==curNode)
  32. "EmptyDocument.\n");
  33. xmlFreeDoc(doc);//释放文件
  34. return-1;
  35. }
  36. if(xmlStrcmp(curNode->name,(constxmlChar*)"log4c"))//确认根元素是否为“log4c”
  37. "Documentofwrongtype.rootnode!=log4c");
  38. xmlFreeDoc(doc);
  39. curNode=curNode->xmlChildrenNode;
  40. xmlNodePtrpropNode=curNode;
  41. while(NULL!=curNode)//遍历所有节点
  42. //获取名称为category的节点
  43. if(!xmlStrcmp(curNode->name,0); background-color:inherit">"category"))
  44. //查找带有属性name的节点
  45. if(xmlHasProp(curNode,BAD_CAST"name"))
  46. propNode=curNode;
  47. //查找属性name为WLAN_Console的节点
  48. xmlAttrPtrattrPtr=propNode->properties;
  49. while(NULL!=attrPtr)//遍历所有名称为category的节点
  50. if(!xmlStrcmp(attrPtr->name,0); background-color:inherit">"name"))//找到有name属性到节点
  51. //查找属性为name的值的节点
  52. xmlChar*szPropity=xmlGetProp(propNode,("name");
  53. if(!xmlStrcmp((constxmlChar*)szPropity,0); background-color:inherit">"WLAN_Console"))
  54. xmlAttrPtrsetAttrPtr=propNode->properties;
  55. while(NULL!=setAttrPtr)
  56. //设置属性priority的值
  57. xmlSetProp(propNode,0); background-color:inherit">"priority",0); background-color:inherit">"debug");
  58. setAttrPtr=setAttrPtr->next;
  59. attrPtr=attrPtr->next;
  60. curNode=curNode->next;
  61. //保存文档到原文档中
  62. xmlSaveFile("log4crc",doc);
  63. "...........OK............\n");
  64. return0;
  65. }

  1. xmlversion="1.0"encoding="UTF-8"?>
  2. <radios>
  3. radio>
  4. name>Bayernname>
  5. url>http://mp3.webradio.antenne.de:80url>
  6. classificationarea>usaarea>
  7. style>musicstyle>
  8. classification>
  9. radio>
  10. >DEU-AntenneBayern>http://testradios>
上代码
    staticxmlXPathObjectPtrgetNodeset(xmlDocPtrdoc,constxmlChar*xpath)
  1. {
  2. xmlXPathContextPtrcontext;
  3. xmlXPathObjectPtrresult;
  4. context=xmlXPathNewContext(doc);
  5. if(context==NULL){
  6. printf("contextisNULL\n");
  7. returnNULL;
  8. }
  9. result=xmlXPathEvalExpression(xpath,context);
  10. xmlXPathFreeContext(context);
  11. if(result==NULL){
  12. printf("xmlXPathEvalExpressionreturnNULL\n");
  13. returnNULL;
  14. }
  15. if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
  16. xmlXPathFreeObject(result);
  17. printf("nodesetisempty\n");
  18. returnresult;
  19. }

playlistDoc 为 xmlDocPtr类型.

copy
    xmlChar*xpath=BAD_CAST("/radios/radio[name='DEU-AntenneBayern']");//关键在这行
  1. xmlXPathObjectPtrapp_result=getNodeset(playlistDoc,xpath);
  2. if(app_result==NULL)
  3. printf("app_resultisNULL\n");
  4. return;
  5. inti=0;
  6. xmlChar*value;
  7. if(app_result)
  8. xmlNodeSetPtrnodeset=app_result->nodesetval;
  9. xmlNodePtrcur;
  10. for(i=0;i<nodeset->nodeNr;i++)
  11. {
  12. cur=nodeset->nodeTab[i];
  13. cur=cur->xmlChildrenNode;
  14. while(cur!=NULL)
  15. if(!xmlStrcmp(cur->name,(constxmlChar*)"name"))
  16. printf("%s\n",((char*)XML_GET_CONTENT(cur->xmlChildrenNode)));
  17. elseconstxmlChar*)"url"))
  18. cur=cur->next;
  19. xmlXPathFreeObject(app_result);
  20. }

输出:

DEU-Antenne Bayern
http://mp3.webradio.antenne.de:80
DEU-Antenne Bayern
http://test


copy
    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']");
  1. EU-AntenneBayern

输出:

http://mp3.webradio.antenne.de:80

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念