1.头文件
#ifndef _XML_H_
#define _XML_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
typedef struct _XML
{
struct Job_Policy JobPolicy;
struct Targer
}
int GetCurFilePath(char *lpOut);
int GetCurDir(char *lpOut);
xmlDocPtr getdoc(char *docname);
xmlXPathObjectPtr getnodeset(xmlDocPtr doc,xmlChar *xpath);
int read_JobProperty(xmlNodePtr job);
int read_JobChildProperty(xmlNodePtr Job,char *flag);
int read_JobPolicy(xmlNodePtr Job);
int read_JobPolicyRelation(xmlNodePtr cur);
int read_TargeResources(xmlNodePtr Target);
void testReadXmlDoc(char *filepath);
#endif
2.代码
#include "xml.h"
int GetCurFilePath(char *lpOut)
{
char chPath[BUFSIZ] = {0};
int nRetVal = readlink("/proc/self/exe",chPath,sizeof(chPath));
if(nRetVal < 0 )
{
strcpy(lpOut,".");
return -1;
}
else
{
strcpy(lpOut,chPath);
return 0;
}
}
int GetCurDir(char *lpOut)
{
char chPath[BUFSIZ] = {0};
if(GetCurFilePath(chPath) < 0)
return -1;
dirname(chPath);
strcpy(lpOut,chPath);
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;
context = xmlXPathNewContext(doc);
if(context == NULL)
{
printf("Error in xmlXPathNewContent\n");
return NULL;
}
result = xmlXPathEvalExpression(xpath,context);
xmlXPa