Linux 下C/C++解析XML文件

      在工程项目中我们的项目需要根据不同的环境配置不同的程序参数,而常用的两种文件分别是ini文件和XML文件,接下来我来分析下在Linux下解析XML文件过程。


      我们首先使用linux自带的libxml2来解析XML文件。

       在libxml2中比较重要的数据结构是xmlNodePtr,它在libxml/tree.h中定义为

/**
 * xmlNode:
 *
 * A node in an XML tree.
 */
typedef struct _xmlNode xmlNode;
typedef xmlNode *xmlNodePtr;
struct _xmlNode {
    void           *_private;	/* application data */
    xmlElementType   type;	/* type number, must be second ! */
    const xmlChar   *name;      /* the name of the node, or the entity */
    struct _xmlNode *children;	/* parent->childs link */
    struct _xmlNode *last;	/* last child link */
    struct _xmlNode *parent;	/* child->parent link */
    struct _xmlNode *next;	/* next sibling link  */
    struct _xmlNode *prev;	/* previous sibling link  */
    struct _xmlDoc  *doc;	/* the containing document */

    /* End of common part */
    xmlNs           *ns;        /* pointer to the associated namespace */
    xmlChar         *content;   /* the content */
    struct _xmlAttr *properties;/* properties list */
    xmlNs           *nsDef;     /* namespace definitions on this node */
    void            *psvi;	/* for type/PSVI informations */
    unsigned short   line;	/* line number */
    unsigned short   extra;	/* extra data for XPath/XSLT */
};

解析代码如下:
#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         int main(int argc, const char *argv[]) { if(2 != argc) { fprintf(stdout, "Please statrt this program with %s xmlfilepath!", argv[0]); return 1; } xmlDocPtr pDoc = xmlReadFile(argv[1], "UTF-8", XML_PARSE_RECOVER); //获取XML文档的指针 if(NULL == pDoc) { fprintf(stderr, "xmlParseFile Error in %s %d\n",__FUNCTION__, __LINE__); return -1; } xmlNodePtr pRoot = xmlDocGetRootElement(pDoc);//获取根节点 if(NULL == pRoot) { fprintf(stderr, "xmlDocGetRootElement Error in %s %d\n", __FUNCTION__, __LINE__); xmlFreeDoc(pDoc); return -1; } printf("Node name is %s!\n", pRoot->name); xmlNodePtr pFirst = pRoot->xmlChildrenNode;//获取子节点 while(NULL != pFirst) { if(!xmlStrcmp(pFirst->name, (const xmlChar *)("monitor"))) { xmlNodePtr pSecond = pFirst->xmlChildrenNode; while(NULL != pSecond) { xmlChar* value= NULL; if(!xmlStrcmp(pSecond->name, (const xmlChar *)("name"))) { value = xmlNodeGetContent(pSecond); printf("\n%s-->%s\n", pSecond->name, value); xmlFree(value); value = NULL; } if(!xmlStrcmp(pSecond->name, (const xmlChar *)("path"))) { value = xmlNodeGetContent(pSecond); printf("\n%s-->%s\n", pSecond->name, value); xmlFree(value); value = NULL; } if(!xmlStrcmp(pSecond->name, (const xmlChar *)("log"))) { xmlNodePtr pThird = pSecond->xmlChildrenNode; while(NULL != pThird) { if(!xmlStrcmp(pThird->name, (const xmlChar *)("folderpath"))) { value = xmlNodeGetContent(pThird); printf("\n%s-->%s\n", pThird->name, value); xmlFree(value); value = NULL; } if(!xmlStrcmp(pThird->name, (const xmlChar *)("savedays"))) { value = xmlNodeGetContent(pThird); printf("\n%s-->%s\n", pThird->name, value); xmlFree(value); value = NULL; } pThird = pThird->next; } } pSecond = pSecond->next; } } pFirst = pFirst->next; } xmlFreeDoc(pDoc); return 0; } 
       
      
      
     
     
    
    
   
   

使用的XML文件如下


	
		ftp   
		/usr/bin/   
		30  
		30  
		127.0.0.1   
		9878    
		1;monitor    
		The ftp is working ok.   
		
			log/  
			2     
		
	


你可能感兴趣的:(C/C++,Linux,CC++解析XML文件)