ubuntu环境下libxml2的测试

22 篇文章 0 订阅
4 篇文章 0 订阅

    一、创建一个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




   


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值