ubuntu环境下libxml2的测试
来源:互联网 发布:qq自动回复软件 编辑:程序博客网 时间:2024/05/20 04:13
一、创建一个xml文件
#include <stdio.h>#include "libxml/parser.h"#include "libxml/tree.h"int main(){ xmlDocPtr doc = NULL; xmlNodePtr root_node = NULL,node = NULL,node1 = NULL; doc = xmlNewDoc(BAD_CAST "1.0"); root_node = xmlNewNode(NULL,BAD_CAST "root"); xmlDocSetRootElement(doc,root_node); xmlNewChild(root_node,NULL,BAD_CAST "node1",BAD_CAST "content 中文"); node = xmlNewChild(root_node,NULL,BAD_CAST "node2",BAD_CAST "has s"); xmlNewProp(node,BAD_CAST "attribute",BAD_CAST "yes"); node = xmlNewNode(NULL,BAD_CAST "node3"); node1 = xmlNewText(BAD_CAST "other way to create content"); xmlAddChild(node,node1); xmlAddChild(root_node,node); xmlSaveFormatFileEnc("-",doc,"UTF-8",1); int ret = xmlSaveFile("xmltest.xml",doc); if(ret != -1) printf("successful\n"); xmlFreeDoc(doc); //xmlCleanupParser(); //xmlMemoryDump(); return 0;}
二、查找节点与解析
#include <stdio.h>#include "libxml/parser.h"#include "libxml/xpath.h"void getNodeset(xmlDocPtr doc,const xmlChar* path){ xmlXPathContextPtr context; xmlXPathObjectPtr result; context = xmlXPathNewContext(doc); if(context == NULL) { fprintf(stderr,"context error\n"); return NULL; } result = xmlXPathEvalExpression(path,context); xmlXPathFreeContext(context); if(result == NULL) { fprintf(stderr,"expression error\n"); return NULL; } if(xmlXPathNodeSetIsEmpty(result->nodesetval)) { xmlXPathFreeObject(result); fprintf(stderr,"empty result\n"); return NULL; } xmlNodeSetPtr nodeset = result->nodesetval; xmlChar* key; int i; for(i = 0;i < nodeset->nodeNr;i++) { key = xmlNodeListGetString(doc,nodeset->nodeTab[i]->xmlChildrenNode,1); printf("keyword:%s\n",key); xmlFree(key); } xmlXPathFreeObject(result); return;}int main(int argc,char** argv){ xmlDocPtr doc; xmlNodePtr node; xmlChar* key; char* docname; if(argc <=1 ) { printf("usage: %s docname\n",argv[0]); return -1; } docname = argv[1]; doc = xmlReadFile(docname,"gb2312",XML_PARSE_RECOVER); if(doc == NULL) { fprintf(stderr,"document parser false\n"); return -1; } getNodeset(doc,"/root/node1"); node = xmlDocGetRootElement(doc); if(node == NULL) { fprintf(stderr,"empty document\n"); xmlFreeDoc(doc); return -1; } if(xmlStrcmp(node->name,BAD_CAST"root")) { fprintf(stderr,"wrong type,root"); xmlFreeDoc(doc); return -1; } node = node->xmlChildrenNode; xmlNodePtr nodeptr = node; while(node != NULL) { if(!xmlStrcmp(node->name,BAD_CAST"node1")) { key = xmlNodeGetContent(node); printf("node:%s\n",key); xmlFree(key); } if(xmlHasProp(node,BAD_CAST"attribute")) { nodeptr = node; } node = node->next; } xmlAttrPtr attrptr = nodeptr->properties; while(attrptr != NULL) { if(!xmlStrcmp(attrptr->name,BAD_CAST"attribute")) { xmlChar* attr = xmlGetProp(nodeptr,BAD_CAST"attribute"); printf("attribure:%s\n",attr); xmlFree(attr); } attrptr = attrptr->next; } xmlFreeDoc(doc); return 0;}三、makefile
INCDIR = -I/usr/include/libxml2LIBDIR= -L/usr/lib/i386-linux-gnuLIBSO = -lxml2CFLAG = -Wall -gall:xml xmlpaserxml:xmlcreate.o gcc ${CFLAG} -o $@ $< ${LIBDIR} ${LIBSO}xmlcreate.o:xmlcreate.c gcc -c -o $@ $^ ${INCDIR}xmlpaser:xmlpaser.o gcc ${CFLAG} -o $@ $< ${LIBDIR} ${LIBSO}xmlpaser.o:xmlpaser.c gcc -c -o $@ $^ ${INCDIR}clear: rm -f *.o
- ubuntu环境下libxml2的测试
- Ubuntu下编译libxml2出错
- windows环境下libxml2编译
- windows环境下libxml2编译
- Linux环境下C使用的XML解析库:libxml2
- Linux环境下C使用的XML解析库:libxml2
- Windows环境下libxml2库的使用小结
- Linux环境下C使用的XML解析库:libxml2
- Windows环境下libxml2库的使用小结
- Ubuntu环境下Thrift的安装、编译以及测试
- Ubuntu环境下Redis的安装与JAVA连接测试
- Ubuntu环境下Neo4j图数据库的安装与测试
- ubuntu下编译和使用libxml2
- ubuntu下编译和使用libxml2
- ubuntu下编译和使用libxml2
- ubuntu下编译和使用libxml2
- ubuntu下编译和使用libxml2
- ubuntu下编译和使用libxml2
- ruby install相关(远程安装时出现HTTP Response 302的解决办法)
- C语言中volatile关键字的作用
- t-interview-x
- 用jprofiler监控rcp程序
- windows 服务
- ubuntu环境下libxml2的测试
- Node.js 体验 — 当 Node.js 邂逅 Windows Azure
- MDCC讲师访谈:热酷CEO刘勇谈移动淘金法门
- taskkill 终止进程的简单实现(源代码)
- RDLC不预览打印——超市小票
- oracle 自然连接,内连接,外链接,全连接,自连接
- STRING.EQUALS()和STRING.EQUALSIGNORECASE()的区别
- ISO/IEC 14443协议浅谈—TYPE A 与 TYPE B 之比较
- 7bit编码解码---未测试