一、创建一个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]->xmlChildrenN
ode,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/libxml2
LIBDIR= -L/usr/lib/i386-linux-gnu
LIBSO = -lxml2
CFLAG = -Wall -g
all:xml xmlpaser
xml: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