找回密码
 注册
关于网站域名变更的通知
查看: 342|回复: 1
打印 上一主题 下一主题

Linux ------- XPath对xml解析

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-8-1 13:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x

& L/ ?5 d9 t' s. L6 a2 z5 P2 O, x8 \0 k* I- q, e$ W; ^% u
  • #ifndef CONF_XML_H
  • #define CONF_XML_H
  • // xml文件Z在《Linux下获取xml调试信息等级》里有
  • #include <stdio.h>
  • #include <string.h>
  • #include <unistd.h>
  • #include <assert.h>
  • #include <libxml/parser.h>
  • #include <libxml/tree.h>
  • #include <libxml/xmlmemory.h>
  • #include <libxml/xpath.h>
  • #define ROOT "root" //根节点
  • #define SON_1 "can0" //儿子节点1
  • #define SON_2 "can1" //儿子节点2
  • #define GRAND_SON "tag" //孙子节点
  • #define GRAND_SON_ATTR "id"
  • #define GREAT_GRANDSON_1 "attr" //曾孙节点1
  • #define GREAT_GRANDSON_2 "goods" //曾孙节点2
  • #define ID_STR_LEN        16
  • #define NAME_STR_LEN      32
  • #define TEL_STR_LEN       16
  • #define ADDR_STR_LEN      128
  • // xml结点结构体
  • typedef struct can_t
  • {
  •   int id;                   // 编号
  •   char attr[NAME_STR_LEN];  //
  •   char goods[TEL_STR_LEN];  //
  • } can;
  • int ConfAddNode(char *son, int id, char *attr, char *goods);
  • int ConfDelNode(char *son, int id);
  • xmlXPathObjectPtr ConfGetNode(xmlDocPtr doc, xmlChar *xpath);
  • #endif
    ' b/ P- }/ s5 P+ N
4 L/ I# A3 T) ^) c3 e

0 y/ Q- P% L% o9 d, o' b) O
  • #include "conf_xml.h"
  • #include "conf_debug.h"
  • static int find_sub_node(xmlNodePtr root, char *son, int id, char *attr, char *goods)
  • {
  •   xmlNodePtr cur = NULL, cur_grandson = NULL;
  •   char cur_id[ID_STR_LEN] = {0};
  •   snprintf(cur_id, ID_STR_LEN, "%d", id);
  •   cur = root->xmlChildrenNode;
  •   while(cur != NULL)
  •   {
  •     if ((!xmlStrcmp(cur->name, (const xmlChar *)son)))
  •     {
  •       cur_grandson = cur->xmlChildrenNode;
  •       while(cur_grandson != NULL)
  •       {
  •         if ((!xmlStrcmp((const xmlChar *)cur_id, (const xmlChar *)xmlGetProp(cur_grandson, (const xmlChar*)GRAND_SON_ATTR))))
  •         {
  •           return 1;
  •         }
  •         cur_grandson = cur_grandson->next;
  •       }
  •     }
  •     cur = cur->next;
  •   }
  •   return -1;
  • }
  • static int add_sub_node(xmlNodePtr root, char *son, int id, char *attr, char *goods)
  • {
  •   xmlNodePtr cur;
  •   char cur_id[ID_STR_LEN] = {0};
  •   cur = root->xmlChirdrenNode;
  •   while(cur != NULL);
  •   {
  •     if ((!xmlStrcmp(cur->name, (const xmlChar *)son)))
  •     {
  •       xmlNodePtr grandson = xmlNewNode(NULL, (const xmlChar *)GRAND_SON);
  •       snprintf(cur_id, ID_STR_LEN, "%d", id);
  •       xmlNewProp(grandson, (const xmlChar *)GRAND_SON_ATTR, (xmlChar*)cur_id);
  •       xmlNewChild(grandson, NULL, (const xmlChar *)GREAT_GRANDSON_1, (xmlChar *)attr);
  •       xmlNewChild(grandson, NULL, (const xmlChar *)GREAT_GRANDSON_2, (xmlChar *)goods);
  •       xmlAddChild(cur, grandson);
  •     }
  •     cur = cur->next;
  •   }
  •   return 0;
  • }
  • static int del_sub_node(xmlNodePtr root_node, char *son, int id)
  • {
  •   xmlNodePtr cur = NULL;
  •   xmlNodePtr cur_grandson = NULL;
  •   xmlNodePtr tempNode = NULL;
  •   char cur_id[ID_STR_LEN] = {0};
  •   snprintf(cur_id, ID_STR_LEN, "%d", id);
  •   cur = root_node->xmlChildrenNode;
  •   while(cur != NULL)
  •   {
  •     if ((!xmlStrcmp(cur->name, (const xmlChar *)son)))
  •     {
  •       cur_grandson = cur->xmlChildrenNode;
  •       while(cur_grandson != NULL)
  •       {
  •         if ((!xmlStrcmp((const xmlChar *)cur_id, (const xmlChar *)xmlGetProp(cur_grandson, (const xmlChar*)GRAND_SON_ATTR))))
  •         {
  •           tempNode = cur_grandson->next;
  •           xmlUnlinkNode(cur_grandson);
  •           xmlFreeNode(cur_grandson);
  •           cur_grandson = tempNode;
  •           continue;
  •         }
  •         cur_grandson = cur_grandson->next;
  •       }
  •     }
  •     cur = cur->next;
  •   }
  •   return 0;
  • }
  • int ConfAddNode(char * son, int id, char * attr, char * goods)
  • {
  •   assert(CONF_FILE_NAME);
  •   xmlDocPtr doc = NULL;
  •   xmlNodePtr root = NULL;
  •   doc = xmlReadFile(CONF_FILE_NAME, "UTF-8", 256); //解析文件
  •   if (doc == NULL)
  •   {
  •     fprintf(stderr, "Failed to parser xml file:%s\n", CONF_FILE_NAME);
  •     return -1;
  •   }
  •   root = xmlDocGetRootElement(doc);
  •   if (root == NULL)
  •   {
  •     fprintf(stderr, "Failed to get root node.\n");
  •     goto FAILED;
  •   }
  •   /*先查找有没有在同一个端口上有同一个id号的出现,如果有,就不要加入了,因为原来就有这个ID*/
  •   if (find_sub_node(root, son, id, attr, goods) == 1)
  •   {
  •     xmlSaveFormatFileEnc(CONF_FILE_NAME, doc, "UTF-8", 1);
  •     xmlFreeDoc(doc);
  •     return 0;
  •   }
  •   if (add_sub_node(root, son, id, attr, goods) != 0)
  •   {
  •     fprintf(stderr, "Failed to add a new can node.\n");
  •     goto FAILED;
  •   }
  •   //将文档保存到文件中,按照utf-8编码格式保存
  •   xmlSaveFormatFileEnc(CONF_FILE_NAME, doc, "UTF-8", 1);
  •   xmlFreeDoc(doc);
  •   return 1;
  • FAILED:
  •   if (doc)
  •   {
  •     xmlFreeDoc(doc);
  •   }
  •   return -1;
  • }
  • int ConfDelNode(char * son, int id)
  • {
  •   assert(CONF_FILE_NAME);
  •   xmlDocPtr doc = NULL;
  •   xmlNodePtr root = NULL;
  •   doc = xmlReadFile(CONF_FILE_NAME, "UTF-8", 256); //解析文件
  •   if (doc == NULL)
  •   {
  •     fprintf(stderr, "Failed to parser xml file:%s\n", CONF_FILE_NAME);
  •     return -1;
  •   }
  •   root = xmlDocGetRootElement(doc);
  •   if (root == NULL)
  •   {
  •     fprintf(stderr, "Failed to get root node.\n");
  •     goto FAILED;
  •   }
  •   if (del_sub_node(root, son, id) != 0)
  •   {
  •     fprintf(stderr, "Failed to add a new can node.\n");
  •     goto FAILED;
  •   }
  •   //将文档保存到文件中,按照utf-8编码格式保存
  •   xmlSaveFormatFileEnc(CONF_FILE_NAME, doc, "UTF-8", 1);
  •   xmlFreeDoc(doc);
  •   return 0;
  • FAILED:
  •   if (doc)
  •   {
  •     xmlFreeDoc(doc);
  •   }
  •   return -1;
  • }
  • xmlXPathObjectPtr ConfGetNode(xmlDocPtr doc, xmlChar * xpath)
  • {
  •   xmlXPathContextPtr context;
  •   xmlXPathObjectPtr result;
  •   context = xmlXPathNewContext(doc);
  •   if (context == NULL)
  •   {
  •     printf("context is NULL\n");
  •     return NULL;
  •   }
  •   result = xmlXPathEvalExpression(xpath, context);
  •   xmlXPathFreeContext(context);
  •   if (result == NULL)
  •   {
  •     printf("xmlXPathEvalExpression return NULL\n");
  •     return NULL;
  •   }
  •   if (xmlXPathNodeSetIsEmpty(result->nodesetval))
  •   {
  •     xmlXPathFreeObject(result);
  •     printf("nodeset is empty\n");
  •     return NULL;
  •   }
  •   return result;
  • }
    3 h/ }3 m* s. M! U
' N5 N1 n3 e5 X3 l7 W
7 H+ E& m8 d+ _

  [( O; Y6 s% @3 v: u, t0 S% H; o  w0 M7 [" y% @- P3 ^
+ \7 I* c" O0 g& t6 l

8 m8 L1 \1 [1 W$ J) n. B3 V) u4 e
4 a  ~# }/ D8 B4 p. \5 P

) v2 k! N: {& l" ]7 o# w: w% |8 `: n
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-11-24 21:01 , Processed in 0.156250 second(s), 23 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表