已阅读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鄂尔多斯东胜区招聘100名社区工作者备考题库及1套参考答案详解
- 供货业协议书范本
- 购房住房协议书模板
- 心内科品管圈决赛第一名成果汇报
- 2024房地产豪宅地产项目年度营销方案【营销计划】【地产营销】
- 高校后勤服务管理规范手册
- 医院信息系统升级项目计划书
- 2025年烟草行业劳动合同(无试用期)协议书
- 购物中心智慧化改造-洞察及研究
- DB42-T 2189-2024 小流域水土保持综合治理项目实施方案编制技术规范
- 2025年老旧小区电梯加装工程进度与质量控制研究报告
- 2025年陕西建筑安管人员ABC1C2考试考核题库及答案
- 在押人员民法典宣传课件
- 北京市西城区2024-2025学年三年级上学期期末语文试题
评论
0/150
提交评论