Libxml2使用实例

Libxml2使用实例

ID:42615674

大小:19.08 KB

页数:6页

时间:2019-09-18

Libxml2使用实例_第1页
Libxml2使用实例_第2页
Libxml2使用实例_第3页
Libxml2使用实例_第4页
Libxml2使用实例_第5页
资源描述:

《Libxml2使用实例》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、XML--Libxml2使用实例一,使用Libxml2生成xml1,编辑生成#include#include#includeintmain(intargc,char**argv){xmlDocPtrdoc=NULL;/*documentpointer*/xmlNodePtrroot_node=NULL,node=NULL,node1=NULL;/*nodepointers*///Createsanewdocument,anodeandsetitasarootnodedoc=xmlNewDoc(BA

2、D_CAST"1.0");root_node=xmlNewNode(NULL,BAD_CAST"root");xmlDocSetRootElement(doc,root_node);//createsanewnode,whichis"attached"aschildnodeofroot_nodenode.xmlNewChild(root_node,NULL,BAD_CAST"node1",BAD_CAST"contentofnode1");//xmlNewProp()createsattributes,whichis"attached"toannode.node=xmlNewC

3、hild(root_node,NULL,BAD_CAST"node3",BAD_CAST"nodehasattributes");xmlNewProp(node,BAD_CAST"attribute",BAD_CAST"yes");//Heregoesanotherwaytocreatenodes.node=xmlNewNode(NULL,BAD_CAST"node4");node1=xmlNewText(BAD_CAST"otherwaytocreatecontent");xmlAddChild(node,node1);xmlAddChild(root_node,node);

4、//DumpingdocumenttostdioorfilexmlSaveFormatFileEnc(argc>1?argv[1]:"-",doc,"UTF-8",1);/*freethedocument*/xmlFreeDoc(doc);xmlCleanupParser();xmlMemoryDump();//debugmemoryforregressiontestsreturn0;}2,编译运行gccmain.c-omain.out-I/usr/include/libxml2-lxml23,生成的xml

5、contentofnode1nodehasattributesotherwaytocreatecontent两个实例,说明如何使用Libxml2遍历xml文档和使Xpath获取特定结点的内容值:程序使用的xml文档如下:contentofnode1nodehasattribute

6、sotherwaytocreatecontent二,遍历程序代1,代码#include#include#includeintmain(intargc,char**argv){xmlDocPtrdoc=NULL;xmlNodePtrcur=NULL;char*name=NULL;char*value=NULL;xmlKeepBlanksDefault(0);if(argc<2){printf("ERROR:argcmustbe2orabov

7、e.");return-1;}//createDomtreedoc=xmlParseFile(argv[1]);if(doc==NULL){printf("ERROR:Loadingxmlfilefailed.");exit(1);}//getrootnodecur=xmlDocGetRootElement(doc);if(cur==NULL){printf("ERROR:emptyfile");xmlFreeDoc(doc);exit(2);}//walkthetreecur=

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。