libxml2 移植使用

1: cd libxml2-2.7.8
2:export PATH=(此处变量为交叉编译工具解压的路径)
3: ./configure --host=mips-linux -fPIC ( -m32 跟编译环境相关   --prefix 输出路劲
4:make
5: make install


将编译出来的lib  和include copy到自己的编译工具链的相应目录 
编译自己SDK  注意.a 和.so 指定方式不同  最好选择自己需要的单独copy 其中一个,由于环境的关系有时候会出现问题。

在自己的Makefile 加入 -lxml2(-l(lib)xml2(.so.2)去头去尾原则)

libxml2使用


代码是我从网上看到的,改了一点点,适合我自己程序

<?xml version="1.0" encoding="utf-8"?>
<file>
	<version>v.ICF.001.0A</version>
	<domain> 
	<ping domain="1">www.google.com</ping>
	<ping domain="2">www.esmarthealth.com</ping>
	</domain>
	<port>12050</port>
	<sensor>
	<shortname sensor="1">xxxxxx</shortname>
	<shortname sensor="2">yyyyyy</shortname>
	<shortname sensor="3">zzzzzzz</shortname>
	</sensor>
</file>


*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <iconv.h>
#include <libxml/encoding.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>  
#include "xml.h"


int copyFile(const char *sourceFileNameWithPath, const char *targetFileNameWithPath)  
{  
    FILE *fpR, *fpW;  
    char buffer[1024];  
    int lenR, lenW;  
    if ((fpR = fopen(sourceFileNameWithPath, "r+")) == NULL)  
    {  
        printf("The file '%s' can not be opened! \n", sourceFileNameWithPath);  
        return -1;  
    }  
    if ((fpW = fopen(targetFileNameWithPath, "w")) == NULL)  
    {  
        printf("The file '%s' can not be opened! \n", targetFileNameWithPath);  
        fclose(fpR);  
        return -1;  
    }  
  
    memset(buffer, 0, 1024);  
    while ((lenR = fread(buffer, 1, 1024, fpR)) > 0)  
    {  
        if ((lenW = fwrite(buffer, 1, lenR, fpW)) != lenR)  
        {  
            printf("Write to file '%s' failed!\n", targetFileNameWithPath);  
            fclose(fpR);  
            fclose(fpW);  
            return -1;  
        }  
        memset(buffer, 0, 1024);  
    }  
  
    fclose(fpR);  
    fclose(fpW);  
    return 0;  
} 
int creatXmlFromRam(void)
{
//FILE  *fpW;
     // save user uid  
/*  uid_t uid = getuid();  
  // get root authorities  
  if(setuid(0)) {  
        printf("test-uid: setuid error");  
        return -1;  
  }  
  printf("test-uid: run as root, setuid is 0\n");  
  //system ("ln -s /bin/abc.xml /bin/xml");
  //system("mount -o remount rw /bin/");
  
  // rollback user authorities  
  if(setuid(uid)) {  
        printf("test-uid: setuid error");  
        return -1;  
  }  
  printf("test-uid: run as user, setuid is %d\n", uid);  
 // system ("ln -s /bin/abc.xml /bin/xml");  
   // 	int ret=symlink("/bin/abc.xml", "/bin/xml");
//	if(ret==-1)
//	{
//		printf("22243423432\n");
//	}
*/
	copyFile("/etc/abc.xml","/var/abc.xml.bak");
/*  if ((fpW = fopen("/etc/abc.xml", "w")) == NULL)  
    {  
       		 printf("The file  can not be opened! \n");  
        	 fclose(fpW);  
        	return -1;  
     	}
	char buff[10];
	//memset(buff,0,sizeof(buff)); 
	int lenW = fwrite(buff,1,sizeof(buff), fpW);
	if(lenW<0)
		{}
	fclose(fpW); 
	*/
	return 0;
}

/* 解析文档 */  
xmlDocPtr getdoc(char *docname){  
    xmlDocPtr doc;  
    doc = xmlParseFile(docname);  
    if(doc == NULL){  
        fprintf(stderr, "Document not parsed successfully. \n");  
        return NULL;  
    }  
  
    return doc;  
}  
  
/* 查询节点集 */  
xmlXPathObjectPtr getnodeset(xmlDocPtr doc, xmlChar *xpath){  
    xmlXPathContextPtr context;  
    xmlXPathObjectPtr result; /* 存储查询结果 */  
  
    /* 创建一个xpath上下文 */  
    context = xmlXPathNewContext(doc);  
    if(context == NULL){  
        printf("Error in xmlXPathNewContext\n");  
        return NULL;  
    }  
    /* 查询XPath表达式 */  
    result = xmlXPathEvalExpression(xpath, context);  
    xmlXPathFreeContext(context); /* 释放上下文指针 */  
    if(result == NULL){  
        printf("Error in xmlXPathEvalExpression\n");  
        return NULL;  
    }  
    /* 检查结果集是否为空 */  
    if(xmlXPathNodeSetIsEmpty(result->nodesetval)){  
        xmlXPathFreeObject(result); /* 如为这空就释放 */  
        printf("No result\n");  
        return NULL;  
    }  
    return result;  
}  
int xmlReadShortName(char **msg){  
    char *docname;  
    xmlDocPtr doc;  
    /* 查找所有keyword元素,而不管它们在文档中的位置 */  
    xmlChar *xpath=(xmlChar*)"//shortname";  
    xmlNodeSetPtr nodeset;  
    xmlXPathObjectPtr result;  
    int i;  
    xmlChar *keyword;  
    docname = "abc.xml";
    doc = getdoc(docname);  
    result = getnodeset(doc, xpath);  
    if(result){  
        /* 得到节点集 */  
        nodeset = result->nodesetval;  
        for(i=0; i < nodeset->nodeNr; i++){ /* 打印每个节点中的内容 */  
            keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);  
            printf("keyword: %s\n", keyword);  
	     memcpy(msg[i],keyword,strlen((char *)keyword));	 
            xmlFree(keyword);  
        }  
        xmlXPathFreeObject(result); /* 释放结果集 */  
    }  
  
    xmlFreeDoc(doc); /* 释放文档树 */  
    xmlCleanupParser(); /* 清除库内存 */  
    return(1);  
}  
  
int xmlReadDomain(char **msg){  
    char *docname;  
    xmlDocPtr doc;  
    /* 查找所有keyword元素,而不管它们在文档中的位置 */  
    xmlChar *xpath=(xmlChar*)"//ping";  
    xmlNodeSetPtr nodeset;  
    xmlXPathObjectPtr result;  
    int i;  
    xmlChar *keyword;  
    docname = "abc.xml";
    doc = getdoc(docname);  
    result = getnodeset(doc, xpath);  
    if(result){  
        /* 得到keyword节点集 */  
        nodeset = result->nodesetval;  
        for(i=0; i < nodeset->nodeNr; i++){ /* 打印每个节点中的内容 */  
            keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);  
            printf("keyword: %s\n", keyword);  
	     memcpy(msg[i],keyword,strlen((char *)keyword));	 
            xmlFree(keyword);  
        }  
        xmlXPathFreeObject(result); /* 释放结果集 */  
    }  
  
    xmlFreeDoc(doc); /* 释放文档树 */  
    xmlCleanupParser(); /* 清除库内存 */  
    return(1);  
}  
#if 0
int xmlReadDomain(char *msg)
{
	xmlDocPtr pdoc = NULL;
	xmlNodePtr proot = NULL, pcur = NULL;
	/*****************打开xml文档********************/
	xmlKeepBlanksDefault(0);//必须加上,防止程序把元素前后的空白文本符号当作一个node
	pdoc = xmlReadFile ("abc.xml", "UTF-8", XML_PARSE_RECOVER);//libxml只能解析UTF-8格式数据
	if (pdoc == NULL)
	{
		printf ("error:can't open file!\n");
		exit (1);
	}
	/*****************获取xml文档对象的根节对象********************/
	proot = xmlDocGetRootElement (pdoc);
	if (proot == NULL)
	{
		printf("error: file is empty!\n");
		exit (1);
	}
	/*****************查找书店中所有书籍的名称********************/
	pcur = proot->xmlChildrenNode;

	while (pcur != NULL)
	{
		//如同标准C中的char类型一样,xmlChar也有动态内存分配,字符串操作等 相关函数。例如xmlMalloc是动态分配内存的函数;xmlFree是配套的释放内存函数;xmlStrcmp是字符串比较函数等。
		//对于char* ch="book", xmlChar* xch=BAD_CAST(ch)或者xmlChar* xch=(const xmlChar *)(ch)
		//对于xmlChar* xch=BAD_CAST("book"),char* ch=(char *)(xch)
		if (!xmlStrcmp(pcur->name, BAD_CAST("domain")))
		{
			xmlNodePtr nptr=pcur->xmlChildrenNode;
			if (!xmlStrcmp(nptr->name, BAD_CAST("ping")))
			{
				printf("title: %s\n",((char*)XML_GET_CONTENT(nptr->xmlChildrenNode)));
				break;
			}
		}
		pcur = pcur->next;
	}
	/*****************释放资源********************/
	xmlFreeDoc (pdoc);
	xmlCleanupParser ();
	xmlMemoryDump ();
	return 0;
}
#endif
int xmlReadPort(char *msg)
{
	xmlDocPtr pdoc = NULL;
	xmlNodePtr proot = NULL, pcur = NULL;
	/*****************打开xml文档********************/
	xmlKeepBlanksDefault(0);//必须加上,防止程序把元素前后的空白文本符号当作一个node
	pdoc = xmlReadFile ("abc.xml", "UTF-8", XML_PARSE_RECOVER);//libxml只能解析UTF-8格式数据
	if (pdoc == NULL)
	{
		printf ("error:can't open file!\n");
		exit (1);
	}
	/*****************获取xml文档对象的根节对象********************/
	proot = xmlDocGetRootElement (pdoc);
	if (proot == NULL)
	{
		printf("error: file is empty!\n");
		exit (1);
	}
	/*****************查找书店中所有书籍的名称********************/
	pcur = proot->xmlChildrenNode;

	while (pcur != NULL)
	{
		//如同标准C中的char类型一样,xmlChar也有动态内存分配,字符串操作等 相关函数。例如xmlMalloc是动态分配内存的函数;xmlFree是配套的释放内存函数;xmlStrcmp是字符串比较函数等。
		//对于char* ch="book", xmlChar* xch=BAD_CAST(ch)或者xmlChar* xch=(const xmlChar *)(ch)
		//对于xmlChar* xch=BAD_CAST("book"),char* ch=(char *)(xch)
		if (!xmlStrcmp(pcur->name, BAD_CAST("port")))
		{
			sprintf(msg,"%s\n",((char*)XML_GET_CONTENT(pcur->xmlChildrenNode)));
			//msg=(char *)XML_GET_CONTENT(pcur->xmlChildrenNode);
		}
		pcur = pcur->next;
	}
	/*****************释放资源********************/
	xmlFreeDoc (pdoc);
	xmlCleanupParser ();
	xmlMemoryDump ();
	return 0;
}
int xmlReadVersion(char *msg)
{
	xmlDocPtr pdoc = NULL;
	xmlNodePtr proot = NULL, pcur = NULL;
	/*****************打开xml文档********************/
	xmlKeepBlanksDefault(0);//必须加上,防止程序把元素前后的空白文本符号当作一个node
	pdoc = xmlReadFile ("abc.xml", "UTF-8", XML_PARSE_RECOVER);//libxml只能解析UTF-8格式数据
	if (pdoc == NULL)
	{
		printf ("error:can't open file!\n");
		exit (1);
	}
	/*****************获取xml文档对象的根节对象********************/
	proot = xmlDocGetRootElement (pdoc);
	if (proot == NULL)
	{
		printf("error: file is empty!\n");
		exit (1);
	}
	/*****************查找书店中所有书籍的名称********************/
	pcur = proot->xmlChildrenNode;

	while (pcur != NULL)
	{
		//如同标准C中的char类型一样,xmlChar也有动态内存分配,字符串操作等 相关函数。例如xmlMalloc是动态分配内存的函数;xmlFree是配套的释放内存函数;xmlStrcmp是字符串比较函数等。
		//对于char* ch="book", xmlChar* xch=BAD_CAST(ch)或者xmlChar* xch=(const xmlChar *)(ch)
		//对于xmlChar* xch=BAD_CAST("book"),char* ch=(char *)(xch)
		if (!xmlStrcmp(pcur->name, BAD_CAST("version")))
		{
			sprintf(msg,"%s\n",((char*)XML_GET_CONTENT(pcur->xmlChildrenNode)));
			//msg=(char *)XML_GET_CONTENT(pcur->xmlChildrenNode);
		}
		pcur = pcur->next;
	}
	/*****************释放资源********************/
	xmlFreeDoc (pdoc);
	xmlCleanupParser ();
	xmlMemoryDump ();
	return 0;
}
#ifndef __XML_H__
#define __XML_H__

/*
char *msg=(char *)malloc(sizeof(char)*10);
memset(msg,'\0',10);
int res=xmlReadPort(msg);
printf("msg=%s\n",msg);
free(msg);
*/
int xmlReadPort(char *msg);
/*
char **msg=(char **)malloc(sizeof(char *)*2);
//	(char **)malloc((sizeof(char *))*2);//为二维数组分配3行
for(i=0;i<2;i++)
{
	 msg[i] = (char *)malloc(sizeof(char)*20);
}
res=xmlReadDomain(msg);
printf("msg=%s\n",msg[0]);
printf("msg=%s\n",msg[1]);
for(i=0;i<2;i++)
	free(msg[i]);
free(msg);
if(res<0)
{

}
*/
int xmlReadDomain(char **msg);
/*
char **msg=(char **)malloc(sizeof(char *)*3);
//	(char **)malloc((sizeof(char *))*2);//为二维数组分配3行
for(i=0;i<3;i++)
{
	 msg[i] = (char *)malloc(sizeof(char)*20);
}
res=xmlReadDomain(msg);
printf("msg=%s\n",msg[0]);
printf("msg=%s\n",msg[1]);
for(i=0;i<3;i++)
	free(msg[i]);
free(msg);
if(res<0)
{

}
*/
int xmlReadShortName(char **msg);
/*
char *msg=(char *)malloc(sizeof(char)*10);
memset(msg,'\0',10);
int res=xmlReadPort(msg);
printf("msg=%s\n",msg);
free(msg);

*/
int xmlReadVersion(char *msg);
int creatXmlFromRam(void);
#endif




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值