




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
XML-Libxml2使用实例一,使用Libxml2生成xml 1,编辑生成#include #include #include int main(int argc, char *argv)xmlDocPtr doc = NULL; /* document pointer */xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;/* node pointers */ Creates a new document, a node and set it as a root nodedoc = xmlNewDoc(BAD_CAST 1.0);root_node = xmlNewNode(NULL, BAD_CAST root);xmlDocSetRootElement(doc, root_node);/creates a new node, which is attached as child node of root_node node. xmlNewChild(root_node, NULL, BAD_CAST node1,BAD_CAST content of node1);/ xmlNewProp() creates attributes, which is attached to an node.node=xmlNewChild(root_node, NULL, BAD_CAST node3, BAD_CASTnode has attributes);xmlNewProp(node, BAD_CAST attribute, BAD_CAST yes);/Here goes another way to create nodes. node = xmlNewNode(NULL, BAD_CAST node4);node1 = xmlNewText(BAD_CASTother way to create content);xmlAddChild(node, node1);xmlAddChild(root_node, node);/Dumping document to stdio or filexmlSaveFormatFileEnc(argc 1 ? argv1 : -, doc, UTF-8, 1);/*free the document */xmlFreeDoc(doc);xmlCleanupParser();xmlMemoryDump();/debug memory for regression testsreturn 0;2,编译运行gcc main.c -o main.out -I /usr/include/libxml2 -lxml23,生成的xml content of node1 node has attributes other way to create content两个实例,说明如何使用Libxml2遍历xml文档和使Xpath获取特定结点的内容值:程序使用的xml文档如下: content of node1 node has attributes other way to create content二,遍历程序代1,代码#include #include #include int main(int argc, char* argv)xmlDocPtr doc=NULL;xmlNodePtr cur=NULL;char* name=NULL;char* value=NULL;xmlKeepBlanksDefault (0); if(argcxmlChildrenNode; /get sub nodewhile(cur !=NULL) name=(char*)(cur-name); value=(char*)xmlNodeGetContent(cur); printf(DEBUG: name is: %s, value is: %sn, name, value); xmlFree(value); cur=cur-next;/ release resource of xml parser in libxml2xmlFreeDoc(doc);xmlCleanupParser();return 0; 输出:DEBUG: name is: node1, value is: content of node1DEBUG: name is: node3, value is: node has attributesDEBUG: name is: node4, value is: other way to create content2,说明:1) 当使用dom树来解析xml文档时,由于默认的方式是把节点间的空白当作第一个子节点,所以为了能和常说的第一个子节点相符,需调用xmlKeepBlanksDefault (0)函数来忽略这种空白。2)对于使用xmlChar* xmlNodeGetContent(xmlNodePtr cur)函数获取节点内容后,必须调用xmlFree()来对所分配的内存进行释放。使用Xpath获取特定结点的内容(使用的xml文档见上面):/#include #include #include #include #include #include int main(int argc, char* argv)xmlDocPtr doc;xmlXPathContextPtr xpathCtx; xmlXPathObjectPtr xpathObj;xmlNodeSetPtr nodeset;char* xpathExpr = /root/node3;char* val=NULL;int size,i;if(argcnodesetval;if(xmlXPathNodeSetIsEmpty(nodeset)printf(WARNING: No such nodes.n); xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); return -4;/get the value size = (nodeset) ? nodeset-nodeNr : 0;for(i = 0; i nodeTabi-xmlChildrenNode, 1); printf(DEBUG: the results are: %sn, val); xmlFree(val);/Cleanup of XPath data xmlXPathFreeObject(xpathObj);xmlXPathFreeContext(xpathCtx); /* free the document */xmlFreeDoc(doc); xmlCleanupParser(); return 0;输出:DEBUG: the results are: node has attributes由于libxml2内部默认的编码方式为utf-8,所以当在xml文档使用中文时必须指明支持中文编码的编码方式(如gb2312),否则在解析和生成时将会报错。另外显示、输入、输出的时候还必须进行编码转换,不然将很有可能出现乱码。如使用 xmlNodeGetContent(xmlNodePtr cur)接口获取一个含有中文的节点内容后,为了能够正常显示,必须将返回值进行编码转换。下面是编码转换函数可参考如下(使用时需要加上头文件iconv.h):/*功能:字符编码转换输入参数:fromCode:转换前的字符编码方式toCode: 转换后的字符编码方式text: 待转换的字符串返回值:成功: 编码方式为toCode的text字符串失败:返回NULL*/const char* encodeConvert(char* fromCode,char* toCode,const char* text) static char bufout1024,*sin,*sout;int length_in,length_out,err;iconv_t c_pt;c_pt=iconv_open(toCode,fromCode);if(c_pt=(iconv_t)-1) couticonv_open failed: fromCode toCodeendl; return NULL; iconv(c_pt,NULL,NULL,NULL,NULL);length_in=strlen(text)+1;lengt
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 开式、闭式泵的工作原理和调试方法
- 交通劝导面试题及答案
- 2025年 广西高校毕业生“三支一扶”计划招募考试试卷附答案
- 手工香皂产品营销活动
- 治未病科健康宣教课件
- 认识自己小学心理健康
- 2025年中国女士格子衬衫行业市场全景分析及前景机遇研判报告
- 学校安全教育汇报材料
- 《数智时代下的供应链管理:理论与实践》课件 第二章 供应链管理焦点与核心理念
- 卷材车间技师培训
- 2025年围产期保健工作计划
- 2024年山东夏季高中学业水平合格考历史试卷真题(含答案详解)
- 战地记者职业生涯规划
- 2022-2023学年北京市怀柔区八年级(下)期末语文试卷
- 夏令营安全管理工作制度
- 5【选必下】高中语文部编版教材选必下册课内文言文精练
- 实验室安全教育课件
- 2024年知识竞赛-《民用爆炸物品安全管理条例》知识竞赛考试近5年真题集锦(频考类试题)带答案
- 国画基础知识题库单选题100道及答案解析
- 《改革开放史》教学大纲
- 透析病人不安腿综合征
评论
0/150
提交评论