libxml2库解析xml文档举例
来源:互联网 发布:淘宝上几十块的人参 编辑:程序博客网 时间:2024/06/02 07:28
转自: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.
- 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 <stdio.h>
- #include "libxml/parser.h"
- #include "libxml/tree.h"
- int 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;
- }
- xml version="1.0" encoding="UTF-8"?>
- <radios>
- <radio>
- <name>Bayernname>
- <url>http://mp3.webradio.antenne.de:80url>
- <classification>
- <area>usaarea>
- <style>musicstyle>
- classification>
- radio>
- <radio>
- <name>DEU-Antenne Bayernname>
- <url>http://mp3.webradio.antenne.de:80url>
- radio>
- <radio>
- <name>DEU-Antenne Bayernname>
- <url>http://testurl>
- radio>
- radios>
- static xmlXPathObjectPtr getNodeset(xmlDocPtr doc, const xmlChar *xpath)
- {
- xmlXPathContextPtr context;
- xmlXPathObjectPtr result;
- context = xmlXPathNewContext(doc);
- if (context == NULL) {
- printf("context is NULL\n");
- return NULL;
- }
- result = xmlXPathEvalExpression(xpath, context);
- xmlXPathFreeContext(context);
- if (result == NULL) {
- printf("xmlXPathEvalExpression return NULL\n");
- return NULL;
- }
- if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
- xmlXPathFreeObject(result);
- printf("nodeset is empty\n");
- return NULL;
- }
- return result;
- }
playlistDoc 为 xmlDocPtr类型.
- xmlChar *xpath = BAD_CAST("/radios/radio[name='DEU-Antenne Bayern']"); //关键在这行
- xmlXPathObjectPtr app_result = getNodeset(playlistDoc, xpath);
- if (app_result == NULL)
- {
- printf("app_result is NULL\n");
- return;
- }
- int i = 0;
- xmlChar *value;
- if(app_result)
- {
- xmlNodeSetPtr nodeset = app_result->nodesetval;
- xmlNodePtr cur;
- for (i=0; i < nodeset->nodeNr; i++)
- {
- cur = nodeset->nodeTab[i];
- cur = cur->xmlChildrenNode;
- while (cur != NULL)
- {
- if (!xmlStrcmp(cur->name, (const xmlChar *)"name"))
- printf("%s\n", ((char*)XML_GET_CONTENT(cur->xmlChildrenNode)));
- else if (!xmlStrcmp(cur->name, (const xmlChar *)"url"))
- printf("%s\n", ((char*)XML_GET_CONTENT(cur->xmlChildrenNode)));
- 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-Antenne Bayern']");
- xmlChar *xpath = BAD_CAST("/radios/radio[name='DEU-Antenne Bayern' and url='http://mp3.webradio.antenne.de:80']");
- EU-Antenne Bayern
输出:
http://mp3.webradio.antenne.de:80
0 0
- libxml2库解析xml文档举例
- 使用Libxml2解析xml
- libxml2解析XML
- libxml2-xml 解析
- libxml2解析xml
- libxml2 解析xml字符串
- libxml2解析xml总结
- [XML文档解析]libxml2对XML文件的创建、解析、查找、修改
- 使用Libxml2操作XML文档
- 使用Libxml2操作XML文档
- 使用Libxml2操作XML文档
- 使用Libxml2操作XML文档
- 使用Libxml2操作XML文档
- 使用Libxml2操作XML文档
- 使用Libxml2操作XML文档
- libxml2 解析文档的例子
- libxml2 xmlTextReader 解析xml实例
- libxml2解析xml文件记录
- POJ 2155 Matrix 【树状数组 更新区间 求点】
- 浅析Android系统中的Activity启动过程
- Oracle之常用FAQ--性能调整
- You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:fetchCompletio
- Git,SoureTree密钥SSH的生成
- libxml2库解析xml文档举例
- 资源依赖 appt -I android.jar -I xxx.apk
- Linux内核中ioremap映射的透彻理解
- Junit基础学习
- Oracle 300问
- 使用RUBY生成二维码
- S_ISREG等几个常见的宏
- Windows查看SID
- Eclipse 中 program arguments 与 VM arguments 的区别