Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1058782
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 20:06:02

编译命令

点击(此处)折叠或打开

  1. cc -I /home/eas/src/libxml/libxml2-2.6.30/include/ -I /home/eas/src/libiconv/libiconv-1.13.1/include/ -L/home/eas/src/libxml/libxml2-2.6.30/.libs -L/home/eas/src/libiconv/libiconv-1.13.1/lib/.libs/ -lxml2 -liconv -lz -lpthread -lm -o xpath xpath.c


点击(此处)折叠或打开

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <string.h>
  4. #include    <math.h>
  5. #include    <ctype.h>
  6. #include    <sys/stat.h>
  7. #include    <unistd.h>
  8. #include    <stdbool.h>
  9. #include    <sys/timeb.h>
  10. #include    <time.h>

  11. #include    <libxml/parser.h>                /* LIBXML2 Header File */
  12. #include    <libxml/tree.h>                    /* LIBXML2 Header File */
  13. #include     <libxml/xmlmemory.h>            /* LIBXML2 Header File */
  14. #include    <libxml/xpath.h>                /* LIBXML2 Header File */
  15. #include    <iconv.h>                        /* LIBXML2 Header File */

  16. #define FMLFLD_NOTARRAY 0 /* 非数组(用于下标参数) */
  17. #define FMLFLD_OPTIONAL 0 /* 可选域 */
  18. #define FMLFLD_REQUIRED 1 /* 必需域 */
  19. #define    SYM_FML_FS    0x40    /* 域分隔符0x1E*/
  20. #define    SYM_FML_NV    ':'     /* 名值分隔符*/
  21. #define    SYM_FML_AN    '#'     /* 数组下标起始符*/
  22. #define RC_SUCC 0
  23. #define RC_NFND -1
  24. #define RC_NSPC -2

  25. #define SZ_DATA_BUS (32*1024)            /* 数据总线宽度 */
  26. typedef char T_DATABUS[SZ_DATA_BUS];    /* 数据总线 */

  27. void FmlDataClear(char *    cFmlData);
  28. char *FmlFieldSeek(const char *cFmlData, const char *cFieldName,int    iIndex);
  29. int    FmlFieldPut(char *cFmlData,int iBufSize,char *cFieldName,int iIndex,char *cFieldValue);
  30. int FmlFieldGet(const char *cFmlData,const char *cFieldName,int    iIndex,    char *cFieldValue,int iValueSize);
  31. int    FmlFieldNum(const char *cFmlData, const char *cFieldName, int *iNum);

  32. int CodeConvert(char* from_charset, char* to_charset, char* inbuf,size_t inlen, char* outbuf, size_t outlen);
  33. char* u2g(char *inbuf);
  34. char* g2u(char *inbuf);
  35. char * StrTrim(char * pstr);
  36. int    GetDateTime(char * cDate, char * cTime);
  37. int JugeFileExist(char *lFileName);

  38. int XmlSetPropByXpath(const xmlDocPtr doc, const xmlChar *xpath, const int number, const xmlChar *name, const xmlChar *value, const char * encoding);
  39. int XmlNodeSetContentByXpath(const xmlDocPtr doc, const xmlChar *xpath, const int number, const xmlChar *content, const char * encoding);
  40. xmlNodePtr GetNodeByXpath(const xmlDocPtr doc, const xmlChar *xpath, const int number);

  41. int main(int argc, char **argv)
  42. {
  43.     if(argc < 2)
  44.     {
  45.         printf("参数个数不对,应为2个!\n");
  46.         return -1;
  47.     }
  48.     
  49.     char filename[1024];
  50.     memset(filename, 0x00, sizeof(filename));
  51.     strcpy(filename, argv[1]);
  52.     StrTrim(filename);
  53.     
  54.     char sParsefile[1024];
  55.     memset(sParsefile, 0x00, sizeof(sParsefile));
  56.     sprintf(sParsefile, "%s%s%s", getenv("HOME"), "/src/xml_fun/", filename);
  57.     StrTrim(sParsefile);
  58.     
  59.     printf("*************************************************************\n");
  60.     int iRet = JugeFileExist(sParsefile);
  61.     if(iRet != RC_SUCC)
  62.     {
  63.         printf("文件[%s]不存在\n", sParsefile);
  64.         return -1;
  65.     }    
  66.     FILE *    fp = NULL;
  67.     if((fp = fopen(sParsefile, "r")) == NULL)
  68.     {
  69.         printf("打开dmz文件[%s]失败\n", sParsefile);
  70.         return -1;
  71.     }
  72.     char sXmlBuf[1024*1000*8];    /*从报文文件中读取的内容*/
  73.     char sChildBuf[10240];        /*存放一次读取的文件内容*/
  74.     memset(sXmlBuf, 0x00, sizeof(sXmlBuf));
  75.     memset(sChildBuf, 0x00, sizeof(sChildBuf));
  76.     while(NULL != fgets(sChildBuf, sizeof(sChildBuf), fp))
  77.     {
  78.         strcat(sXmlBuf, sChildBuf);
  79.         memset(sChildBuf, 0, sizeof(sChildBuf));
  80.     }
  81.     int iXmlByteNum = strlen(sXmlBuf);
  82.     printf("读取文件[%s],文件长度=[%d]\n", sParsefile, iXmlByteNum);    
  83.     printf("文件数据如下:\n[%s]", sXmlBuf);    
  84.     printf("*************************************************************\n");
  85.     
  86.     
  87.     xmlDocPtr doc = xmlReadFile(sParsefile,"GBK",XML_PARSE_NOBLANKS);
  88.     if (NULL == doc)
  89.     {
  90.         printf("未指定文件名\n");
  91.         return -1;
  92.     }
  93.     
  94.     xmlNodePtr root_node = xmlDocGetRootElement(doc);
  95.     /*检查确认当前文档中包含内容*/
  96.     if (NULL == root_node)
  97.     {
  98.         printf("empty document\n");
  99.         xmlFreeDoc(doc);
  100.         return -1;
  101.     }
  102.     
  103.     
  104.     #ifdef __DEBUG__
  105.     
  106.     printf("*************************************************************\n");
  107.     char xPath[128];
  108.     memset(xPath, 0, sizeof(xPath));
  109.     strcpy(xPath,"/ap/head/tr_code");
  110.     xmlNodePtr cur_node1;
  111.     cur_node1 = GetNodeByXpath(doc,BAD_CAST xPath,0);
  112.     char * nodename1;
  113.     char * nodevalue1;
  114.     nodename1 = u2g((char*)cur_node1->name);
  115.     xmlChar * szAttr1 = xmlNodeGetContent(cur_node1);
  116.     nodevalue1 = u2g((char*)szAttr1);
  117.     printf("nodename1 = [%s]\n", nodename1);
  118.     printf("nodevalue1 = [%s]\n", nodevalue1);
  119.     
  120.     printf("1**************************\n");
  121.     memset(xPath, 0, sizeof(xPath));
  122.     strcpy(xPath,"/ap/head/tr_code");
  123.     char content[128];
  124.     memset(content, 0, sizeof(content));
  125.     strcpy(content, "不知道是什么交易码");
  126.     XmlNodeSetContentByXpath(doc, xPath, 0, content, "GBK");
  127.     cur_node1 = GetNodeByXpath(doc, BAD_CAST xPath, 0);
  128.     nodename1 = u2g((char*)cur_node1->name);
  129.     szAttr1 = xmlNodeGetContent(cur_node1);
  130.     nodevalue1 = u2g((char*)szAttr1);
  131.     printf("nodename1 = [%s]\n", nodename1);
  132.     printf("nodevalue1 = [%s]\n", nodevalue1);
  133.     
  134.     printf("1**************************\n");
  135.     memset(xPath, 0, sizeof(xPath));
  136.     strcpy(xPath,"/ap/head/sign");
  137.     XmlSetPropByXpath(doc, BAD_CAST xPath, 0, "size","size是测试用的属性", "gb2312");
  138.     
  139.      printf("******************************************\n");
  140.     //存储xml文件
  141.     char    date[32];
  142.     char    time[32];
  143.     char    filename1[128];
  144.     memset(date,0,sizeof(date));
  145.     memset(time,0,sizeof(time));
  146.     memset(filename1,0,sizeof(filename1));
  147.     printf("111******************************************\n");
  148.     GetDateTime(date,time);
  149.     StrTrim(date);
  150.     StrTrim(time);
  151.     printf("222******************************************\n");
  152.     sprintf(filename1, "Created_%s_%s.xml", date, time);
  153.     printf("333******************************************\n");
  154.     /*iRet = xmlSaveFormatFileEnc(filename, doc, "GBK", 1);*/
  155.     /*iRet = xmlSaveFile(filename,doc);*/
  156.     /*iRet = xmlSaveFormatFile(filename, doc, 1);*/
  157.     /*xmlOutputBufferPtr buf;
  158.     iRet = xmlSaveFileTo(buf, doc, "GBK"); */
  159.     /*iRet = xmlSaveFileEnc(filename, doc, "GBK"); */
  160.     iRet = xmlSaveFormatFileEnc(filename1, doc, "GB2312", 1);
  161.     if (iRet != -1)
  162.     {
  163.        printf("CreatedXml.xml文件已创建成功\n");
  164.     }
  165.     
  166.     /*
  167.     cur_node1 = GetNodeByXpath(doc,BAD_CAST xPath,0);
  168.     nodename1 = u2g((char*)cur_node1->name);
  169.     szAttr1 = xmlNodeGetContent(cur_node1);
  170.     nodevalue1 = u2g((char*)szAttr1);
  171.     printf("nodename1 = [%s]\n", nodename1);
  172.     printf("nodevalue1 = [%s]\n", nodevalue1);
  173.     */
  174.     printf("1**************************\n");
  175.     
  176.     #endif
  177.     
  178.     printf("*************************************************************\n");
  179.     /*xmlXPathContextPtr:XPATH上下文指针*/
  180.     /*xmlXPathNewContext函数功能:创建一个XPath上下文指针
  181.     函数原型:xmlXPathContextPtr xmlXPathNewContext(xmlDocPtr doc);*/
  182.     xmlXPathContextPtr context = xmlXPathNewContext(doc);
  183.     if (context == NULL)
  184.     {
  185.        printf("context is NULL\n");
  186.        return -1;
  187.     }
  188.     /*xmlXPathContextPtr里面的节点指针成员是什么?*/
  189.     
  190.     /*xmlXPathObjectPtr:XPATH对象指针,用来存储查询结果
  191.     xmlXPathEvalPredicate函数功能*/
  192.     /*xmlXPathEvalExpression函数功能:查询XPath表达式,得到一个查询结果
  193.     函数原型: xmlXPathObjectPtr xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt);
  194.              xmlXPathObjectPtr xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx);
  195.              int xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res);*/
  196.     char xPath2[1024];
  197.     memset(xPath2, 0, sizeof(xPath2));
  198.     strcpy(xPath2,"/request/balance_query/row");
  199.     xmlXPathObjectPtr resultObject = xmlXPathEvalExpression(BAD_CAST xPath2, context);
  200.     if (resultObject == NULL)
  201.     {
  202.        printf("xmlXPathEvalExpression return NULL\n");
  203.        return -1;
  204.     }
  205.     
  206.     xmlXPathObjectPtr resultObject2 = xmlXPathObjectCopy(resultObject);
  207.     
  208.     /*xmlXPathFreeContext函数功能:释放上下文指针*/
  209.     xmlXPathFreeContext(context);
  210.     
  211.     /*result->nodesetval:得到节点集合指针,其中包含了所有符合Xpath查询结果的节点*/
  212.     if (xmlXPathNodeSetIsEmpty(resultObject2->nodesetval) != 0) //检查查询结果是否为空
  213.     {
  214.        xmlXPathFreeObject(resultObject2);
  215.        printf("节点集合是空的!\n");
  216.        return -1;
  217.     }
  218.     /*获取节点集合指针*/
  219.     int boolVal = resultObject2->boolval;
  220.     printf("boolVal = [%d]\n", boolVal);
  221.     double floatVal = resultObject2->floatval;
  222.     printf("floatVal = [%f]\n", floatVal);
  223.     /*
  224.     char stringVal[1024];
  225.     memset(stringVal, 0, sizeof(stringVal));
  226.     strcpy(stringVal, (char*)resultObject->stringval);
  227.     printf("stringVal = [%s]\n", stringVal);
  228.     */
  229.     int indexVal = resultObject2->index;
  230.     printf("indexVal = [%d]\n", indexVal);
  231.     xmlNodeSetPtr node_set = resultObject2->nodesetval;
  232.     int maxvalue = node_set->nodeMax; /*集合最大容量*/
  233.     printf("maxvalue=[%d]\n", maxvalue);
  234.     int nodeNr = node_set->nodeNr; /*查询结果的数量*/
  235.     printf("nodeNr=[%d]\n", nodeNr);
  236.     
  237.     xmlNodePtr new_node1 = xmlNewNode(NULL, BAD_CAST "row");
  238.     xmlNodePtr new_node2 = xmlNewNode(NULL, BAD_CAST "row");
  239.     int cmpFlag = -100;
  240.     cmpFlag = xmlXPathCmpNodes(new_node1, new_node2); /*还没有加入任何doc文档,所以是-2*/
  241.     printf("111***********cmpFlag = [%d]***********\n", cmpFlag);
  242.     cmpFlag = xmlXPathCmpNodes(new_node1, new_node1); /*完全同名的节点*/
  243.     printf("222***********cmpFlag = [%d]***********\n", cmpFlag);
  244.     cmpFlag = xmlXPathCmpNodes(new_node1, node_set->nodeTab[0]);
  245.     printf("333***********cmpFlag = [%d]***********\n", cmpFlag);
  246.     cmpFlag = xmlXPathCmpNodes(node_set->nodeTab[0], node_set->nodeTab[0]);
  247.     printf("444***********cmpFlag = [%d]***********\n", cmpFlag);
  248.     cmpFlag = xmlXPathCmpNodes(node_set->nodeTab[0], node_set->nodeTab[1]);
  249.     printf("4.1***********cmpFlag = [%d]***********\n", cmpFlag);
  250.     cmpFlag = xmlXPathCmpNodes(node_set->nodeTab[1], node_set->nodeTab[0]);
  251.     printf("4.5***********cmpFlag = [%d]***********\n", cmpFlag);
  252.     cmpFlag = xmlXPathCmpNodes(node_set->nodeTab[1], root_node->children->children);
  253.     printf("555***********cmpFlag = [%d]***********\n", cmpFlag);
  254.     cmpFlag = xmlXPathCmpNodes(node_set->nodeTab[0], root_node->children->children);
  255.     printf("5.5***********cmpFlag = [%d]***********\n", cmpFlag);
  256.     cmpFlag = xmlXPathCmpNodes(node_set->nodeTab[0], node_set->nodeTab[2]);
  257.     printf("666***********cmpFlag = [%d]***********\n", cmpFlag);
  258.     cmpFlag = xmlXPathCmpNodes(node_set->nodeTab[2], node_set->nodeTab[0]);
  259.     printf("6.5***********cmpFlag = [%d]***********\n", cmpFlag);
  260.     cmpFlag = xmlXPathCmpNodes(node_set->nodeTab[0], node_set->nodeTab[4]);
  261.     printf("777***********cmpFlag = [%d]***********\n", cmpFlag);
  262.     cmpFlag = xmlXPathCmpNodes(node_set->nodeTab[0], node_set->nodeTab[9]);
  263.     printf("888***********cmpFlag = [%d]***********\n", cmpFlag);
  264.     cmpFlag = xmlXPathCmpNodes(node_set->nodeTab[0], node_set->nodeTab[90]);
  265.     printf("999***********cmpFlag = [%d]***********\n", cmpFlag);
  266.     cmpFlag = xmlXPathCmpNodes(node_set->nodeTab[90], node_set->nodeTab[0]);
  267.     printf("9.5***********cmpFlag = [%d]***********\n", cmpFlag);
  268.     cmpFlag = xmlXPathCmpNodes(node_set->nodeTab[0], root_node->children->next);
  269.     printf("10***********cmpFlag = [%d]***********\n", cmpFlag);
  270.     cmpFlag = xmlXPathCmpNodes(root_node->children->next, node_set->nodeTab[0]);
  271.     printf("10.5***********cmpFlag = [%d]***********\n", cmpFlag);
  272.     
  273.     /*获取节点集合中一个节点的指针*/
  274.     T_DATABUS tDataBus;
  275.     FmlDataClear(tDataBus);
  276.     xmlNodePtr cur_node;
  277.     char * nodename;
  278.     
  279.     xmlChar * szAttr;
  280.     char * eleName;
  281.     char * eleValue;
  282.     xmlAttrPtr attrPtr;
  283.     int i = -1;
  284.     for(i=0; i<nodeNr; i++)
  285.     {
  286.         cur_node = node_set->nodeTab[i];
  287.         nodename = u2g((char*)cur_node->name);
  288.         /*printf("nodename = [%s]\n", nodename);*/
  289.         
  290.      if( xmlStrcmp(cur_node->name,BAD_CAST "row") == 0 )
  291.         {
  292.             for( attrPtr =cur_node->properties ; attrPtr != NULL ; attrPtr = attrPtr->next )
  293.             {
  294.                 szAttr = xmlGetProp(cur_node,attrPtr->name);
  295.                 eleName = u2g((char*)attrPtr->name);
  296.                 eleValue = u2g((char*)szAttr);
  297.                 if(eleValue != NULL)
  298.                     FmlFieldPut(tDataBus, SZ_DATA_BUS, eleName, i, eleValue);
  299.                 else
  300.                     FmlFieldPut(tDataBus, SZ_DATA_BUS, eleName, i, "");
  301.                 xmlFree(szAttr);
  302.                 if( eleName != NULL )
  303.                 {
  304.                     free(eleName);
  305.                     eleName = NULL;
  306.                 }
  307.                 if( eleValue != NULL )
  308.                 {
  309.                     free(eleValue);
  310.                     eleValue = NULL;
  311.                 }
  312.             }
  313.         }
  314.         else
  315.         {
  316.             printf("root node != row\n");
  317.         }
  318.     }
  319.     /*
  320.     typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs);
  321.     typedef struct _xmlXPathFunct xmlXPathFunct;
  322.     typedef xmlXPathFunct *xmlXPathFuncPtr;
  323.     struct _xmlXPathFunct {
  324.         const xmlChar *name; the function name
  325.         xmlXPathEvalFunc func; the evaluation function
  326.     };
  327.     xmlXPathObjectPtr xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx);
  328.     */
  329.     

  330.     /*xmlXPathFreeObject函数功能:释放Xpath对象指针*/
  331.     xmlXPathFreeObject(resultObject);
  332.     xmlFreeDoc(doc);
  333.     printf("总线=[%s]\n", tDataBus);
  334.     
  335.     printf("*************************************************************\n");
  336.     return 0;
  337. }


  338. /*
  339. void xmlXPathFreeNodeSet(xmlNodeSetPtr obj);
  340. xmlXPathObjectPtr xmlXPathObjectCopy(xmlXPathObjectPtr val);
  341. void xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj);

  342. xmlNodeSetPtr xmlXPathNodeSetCreate(xmlNodePtr val);
  343. int xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2);*/


  344. /*************************************************************
  345.  * 函 数 名:XmlSetPropByXpath()
  346.  * 功能描述:将val_str设置到XML文档的xpath节点中
  347.  * 输入参数:doc - XML文档指针
  348.  *          encode - 0:utf8 1:gbk
  349.  *          xpath - XML文档路径,如"/root/abc"
  350.  *          val_str - UTF-8编码的值
  351.  *          number - 如果该XML文档中有多个xpath,给第几个xpath设置,0表示第一个节点
  352.  * 输出参数:无
  353.  * 返 回:0 - 成功 ;-1 - 失败
  354.  * 说 明:如果encode为0不需要转码,如果encode为1,说明是中文GBK编码,需要转换成UTF8
  355.  ************************************************************/
  356. int XmlSetPropByXpath(const xmlDocPtr doc, const xmlChar *xpath, const int number, const xmlChar *name, const xmlChar *value, const char * encoding)
  357. {
  358.     int encodingflag = 1;
  359.     if((strncasecmp(encoding, "GBK", strlen("GBK")) == 0)||(strncasecmp(encoding, "GB2312", strlen("GB2312")) == 0)) /*需要转换为utf-8*/
  360.     {
  361.         encodingflag = 1;
  362.     }
  363.     else
  364.     {
  365.         encodingflag = 0;
  366.     }
  367.     
  368.     xmlXPathContextPtr context = NULL;
  369.     xmlXPathObjectPtr resultObject = NULL;

  370.     context = xmlXPathNewContext(doc);
  371.     resultObject = xmlXPathEval(xpath, context);
  372.     /*number介于0~cur->nodesetval->nodeNr-1之间*/
  373.     int resultNum = resultObject->nodesetval->nodeNr;
  374.     if(resultNum<1 || number>=resultNum || number < 0)
  375.     {
  376.         xmlXPathFreeObject(resultObject);
  377.         xmlXPathFreeContext(context);
  378.         printf("指定xpath的节点不存在!\n");
  379.         return -1;
  380.     }
  381.     xmlNodePtr node = NULL;
  382.     node = resultObject->nodesetval->nodeTab[number];
  383.     if(node == NULL)
  384.     {
  385.         xmlXPathFreeObject(resultObject);
  386.         xmlXPathFreeContext(context);
  387.         printf("指定xpath的节点不存在!\n");
  388.         return -1;
  389.     }
  390.     xmlChar *AttrName = NULL;
  391.     xmlChar *AttrValue = NULL;
  392.     if((name != NULL)&&(value != NULL)) /*均非空*/
  393.     {
  394.         if(encodingflag == 1) /*需要转换为国标码*/
  395.         {
  396.             AttrName = (xmlChar *)g2u((char *)name);
  397.             AttrValue = (xmlChar *)g2u((char *)value);
  398.             if(AttrValue != NULL)
  399.                 xmlSetProp(node,AttrName,AttrValue);
  400.         }
  401.         else /*其他编码*/
  402.         {
  403.             xmlSetProp(node,name,value);
  404.         }
  405.     }
  406.     else if((name != NULL)&&(value == NULL)) /*值为空*/
  407.     {
  408.         if(encodingflag == 1) /*需要转换为国标码*/
  409.         {
  410.             AttrName = (xmlChar *)g2u((char *)name);
  411.             xmlSetProp(node, AttrName, "");
  412.         }
  413.         else /*其他编码*/
  414.         {
  415.             xmlSetProp(node, name, "");
  416.         }
  417.     }

  418.     xmlXPathFreeObject(resultObject);
  419.     xmlXPathFreeContext(context);
  420.     if(AttrName != NULL)
  421.     {
  422.         xmlFree(AttrName);
  423.         AttrName = NULL;
  424.     }
  425.     if(AttrValue != NULL)
  426.     {
  427.         xmlFree(AttrValue);
  428.         AttrValue = NULL;
  429.     }
  430.     return 0;
  431. }


  432. /*************************************************************
  433.  * 函 数 名:XmlNodeSetContentByXpath()
  434.  * 功能描述:将content设置到XML文档的xpath节点中
  435.  * 输入参数:doc - XML文档指针
  436.  *          encode - val_str所采用的编码方式
  437.  *          xpath - XML文档路径,如"/root/abc"
  438.  *          val_str - UTF-8编码的值
  439.  *          number - 如果该XML文档中有多个xpath,给第几个xpath设置,0表示第一个节点
  440.  * 输出参数:无
  441.  * 返 回:0 - 成功 ;-1 - 失败
  442.  * 说 明:如果encode为0不需要转码,如果encode为1,说明是中文GBK编码,需要转换成UTF8
  443.  ************************************************************/
  444. int XmlNodeSetContentByXpath(const xmlDocPtr doc, const xmlChar *xpath, const int number, const xmlChar *content, const char * encoding)
  445. {
  446.     int encodingflag = -1;
  447.     if((strncasecmp(encoding, "GBK", strlen("GBK")) == 0)||(strncasecmp(encoding, "GB2312", strlen("GB2312")) == 0)) /*国标码需要转换为utf-8*/
  448.     {
  449.         encodingflag = 1;
  450.     }
  451.     else
  452.     {
  453.         encodingflag = 0;
  454.     }
  455.     
  456.     xmlXPathContextPtr context = NULL;
  457.     xmlXPathObjectPtr resultObject = NULL;

  458.     context = xmlXPathNewContext(doc);
  459.     resultObject = xmlXPathEval(xpath, context);
  460.     xmlNodeSetPtr nodesetVal = resultObject->nodesetval; /*节点集合指针*/
  461.     /*number介于0~cur->nodesetval->nodeNr-1之间*/
  462.     int resultNum = nodesetVal->nodeNr;
  463.     if(resultNum<1 || number>=resultNum || number < 0)
  464.     {
  465.         xmlXPathFreeObject(resultObject);
  466.         xmlXPathFreeContext(context);
  467.         printf("指定xpath的节点不存在!\n");
  468.         return -1;
  469.     }
  470.     xmlNodePtr node = NULL;
  471.     /*node = (nodesetVal->nodeTab)[number];*/
  472.     node = *((nodesetVal->nodeTab)+number);
  473.     if(node == NULL)
  474.     {
  475.         xmlXPathFreeObject(resultObject);
  476.         xmlXPathFreeContext(context);
  477.         printf("指定xpath的节点不存在!\n");
  478.         return -1;
  479.     }
  480.     xmlChar *out = NULL;
  481.     if(content != NULL) /*空值*/
  482.     {
  483.         if(encodingflag == 1) /*需要转换为utf-8*/
  484.         {
  485.             out = (xmlChar *)g2u((char *)content);
  486.             if(out != NULL)
  487.                 xmlNodeSetContent(node,out);
  488.         }
  489.         else /*其他编码*/
  490.         {
  491.             xmlNodeSetContent(node,content);
  492.         }
  493.     }
  494.     else
  495.     {
  496.         xmlNodeSetContent(node,"");
  497.     }

  498.     xmlXPathFreeObject(resultObject);
  499.     xmlXPathFreeContext(context);
  500.     if(out != NULL)
  501.     {
  502.         xmlFree(out);
  503.         out = NULL;
  504.     }
  505.     return 0;
  506. }


  507. /*************************************************************
  508.  * 函 数 名:GetNodeByXpath()
  509.  * 功能描述: 根据xpath查询指定节点
  510.  * 输入参数:doc        - 文档
  511.  *             xpath        - 节点路径"/request/balance/row"
  512.  *             number    - 序号"/request/balance/row...row...row"
  513.  * 输出参数:
  514.  * 返 回:xmlNode - 成功 ;NULL - 失败
  515.  * 说 明:按序号查找节点
  516.  ************************************************************/
  517. xmlNodePtr GetNodeByXpath(const xmlDocPtr doc, const xmlChar *xpath, const int number)
  518. {
  519.     xmlXPathContextPtr context = NULL;
  520.     xmlXPathObjectPtr resultObject = NULL;

  521.     context = xmlXPathNewContext(doc);
  522.     resultObject = xmlXPathEval(xpath,context);
  523.     /*number介于0~cur->nodesetval->nodeNr-1之间*/
  524.     int resultNum = resultObject->nodesetval->nodeNr;
  525.     if(resultNum<1 || number>=resultNum || number < 0)
  526.     {
  527.         xmlXPathFreeObject(resultObject);
  528.         xmlXPathFreeContext(context);
  529.         printf("指定xpath的节点不存在!\n");
  530.         return (NULL);
  531.     }
  532.     xmlNodePtr node;
  533.     node = resultObject->nodesetval->nodeTab[number];

  534.     xmlXPathFreeObject(resultObject);
  535.     xmlXPathFreeContext(context);

  536.     return (node);
  537. }


  538. /***********************************************************
  539. * 函 数 名:GetDateTime()
  540. * 功能描述: 分别获取系统日期时间
  541. * 输入参数:
  542. *            cDate-日期接收缓冲区;
  543. *            cTime-日期接收缓冲区;
  544. * 输出参数:cDate
  545. * 返 回:0 成功
  546. * 流程描述:
  547. * 说明: 返回日期格式:YYYYMMDD
  548. * 修改记录:
  549. * [修改人] [日期] - [描述]
  550. ***********************************************************/
  551. int    GetDateTime(char * cDate, char * cTime)
  552. {
  553.     struct tm *    cur;
  554.     time_t    timep;

  555.     struct timeb time_msec;
  556.     unsigned short msec;

  557.     time(&timep);
  558.     cur = localtime(&timep);

  559.     ftime(&time_msec);
  560.     msec=time_msec.millitm;

  561.     sprintf(cDate,"%04d%02d%02d",cur->tm_year+1900,cur->tm_mon+1,cur->tm_mday);
  562.     cDate[8]=0;
  563.     sprintf(cTime,"%02d%02d%02d%03d",cur->tm_hour,cur->tm_min,cur->tm_sec,msec);
  564.     cTime[9]=0;

  565.     return 0;
  566. }


  567. int CodeConvert(char* from_charset, char* to_charset, char* inbuf,size_t inlen, char* outbuf, size_t outlen)
  568. {
  569.     iconv_t cd;
  570.     char** pin = &inbuf;
  571.     char** pout = &outbuf;
  572.     cd = iconv_open(to_charset,from_charset);
  573.     if(cd == (iconv_t)-1)
  574.         return -1;
  575.     memset(outbuf,0,outlen);
  576.     if(iconv(cd,(char**)pin,(size_t *)&inlen,(char**)pout,(size_t*)&outlen) == -1)
  577.         return -1;
  578.     iconv_close(cd);
  579.     return 0;
  580. }

  581. /*************************************************************
  582.  * 函 数 名:u2g()
  583.  * 功能描述: UTF-8码转成GBK码
  584.  * 输入参数:inbuf    - 转入字符
  585.  * 输出参数:
  586.  * 返 回:szOut - 成功; NULL - 失败
  587.  * 说 明:成功则返回一个动态分配的char*变量,需要在使用
  588.  *             完毕后手动free,失败返回NULL,使用有风险,调用需谨慎
  589.  ************************************************************/
  590. char* u2g(char *inbuf)
  591. {
  592.     size_t nOutLen = 0;
  593.     char* szOut = NULL;
  594.     if(!strlen(inbuf))
  595.         return NULL;
  596.     nOutLen = 2 * strlen(inbuf) - 1;
  597.     szOut = (char*)malloc(nOutLen);
  598.     if (-1 == CodeConvert("UTF-8","GBK",inbuf,strlen(inbuf),szOut,nOutLen))
  599.     {
  600.         free(szOut);
  601.         szOut = NULL;
  602.     }
  603.     return szOut;
  604. }

  605. /*************************************************************
  606.  * 函 数 名:g2u()
  607.  * 功能描述: GBK码转成UTF-8码
  608.  * 输入参数:inbuf    - 转入字符
  609.  * 输出参数:
  610.  * 返 回:szOut - 成功; NULL - 失败
  611.  * 说 明:成功则返回一个动态分配的char*变量,需要在使用
  612.  *             完毕后手动free,失败返回NULL,使用有风险,调用需谨慎
  613.  ************************************************************/
  614. char* g2u(char *inbuf)
  615. {
  616.     size_t nOutLen = 0;
  617.     char* szOut = NULL;
  618.     if(!strlen(inbuf))
  619.         return NULL;
  620.     nOutLen = 2 * strlen(inbuf) - 1;
  621.     szOut = (char*)malloc(nOutLen);
  622.     if (-1 == CodeConvert("GBK","UTF-8",inbuf,strlen(inbuf),szOut,nOutLen))
  623.     {
  624.         free(szOut);
  625.         szOut = NULL;
  626.     }
  627.     return szOut;
  628. }

  629. /*FmlDataClear 函数功能:清空数据总线函数*/
  630. void FmlDataClear(char *    cFmlData)
  631. {
  632.     cFmlData[0] = 0;
  633. }

  634. /*FmlFieldErase函数:擦除总线数据中的指定域,没有这个域就不擦除*/
  635. void    FmlFieldErase(char *cFmlData, const char *cFieldName,int iIndex)
  636. {
  637.     char    *p, *q;
  638.     if ( ( p = FmlFieldSeek( cFmlData, cFieldName, iIndex ) ) == NULL )
  639.         return; /*p指向指定的域名的起始地址*/
  640.     if ( ( q = strchr( p, SYM_FML_FS ) ) == NULL ) /*SYM_FML_FS:域分割符^^*/
  641.         q = p + strlen( p );
  642.     else
  643.         q ++; /*q指向域尾*/
  644.     strcpy( p, q ); /*p到q之间的被跳过*/
  645. }

  646. /*FmlFieldSeek函数功能:检索总线中指定域函数,返回指定域的起始地址*/
  647. char *FmlFieldSeek(const char *cFmlData, const char *cFieldName,int    iIndex)
  648. {
  649.     char    *p = (char *) cFmlData;
  650.     char    *n, *q, *v;
  651.     int    r;

  652.     while ( ( n = strchr( p, SYM_FML_FS ) ) != NULL )/*SYM_FML_FS:域分割符^^*/
  653.     {    
  654.         *n = 0; /*n指向下一个域分割符^^,并截断*/
  655.         q = strchr( p, SYM_FML_NV ); /*SYM_FML_NV:名值分割符:*/
  656.         *n = SYM_FML_FS; /*恢复域分割符,重新连上总线*/
  657.         if ( q != NULL )
  658.         {
  659.             *q = 0; /*q指向名值分割符:,并截断*/
  660.             if ( ( v = strchr( p, SYM_FML_AN ) ) != NULL ) /*0下标时,v=NULL*/
  661.                 *v = 0; /*v指向数组下标起始符#,并截断*/
  662.             if ( ( r = strcmp( p, cFieldName ) ) == 0 ) /*比较p指向的是不是待检索的域名*/
  663.             {
  664.                 if ( v == NULL && iIndex != FMLFLD_NOTARRAY || v != NULL && atoi( v + 1 ) != iIndex )
  665.                     r = -1; /*对下标值的检查,出错时r=-1*/
  666.             }
  667.             if ( v != NULL )
  668.                 *v = SYM_FML_AN; /*重新连上总线*/
  669.             *q = SYM_FML_NV; /*重新连上总线*/
  670.             if ( r == 0 ) /*域名匹配,下标也正确时,查找完毕*/
  671.                 return p; /*返回域名起始地址*/
  672.         }
  673.         p = n + 1; /*p指向下一个域名起始地址,继续循环查找*/
  674.     }
  675.     return NULL;
  676. }

  677. /*FmlFieldPut() 功能描述: 为总线中指定域赋值函数*/
  678. int    FmlFieldPut(char *cFmlData,int iBufSize,char *cFieldName,int iIndex,char *cFieldValue)
  679. {
  680.     int    s, l;
  681.     char    a[12];

  682.     FmlFieldErase( cFmlData, cFieldName, iIndex ); /*擦除指定域名域值*/

  683.     if ( iIndex == FMLFLD_NOTARRAY ) /*FMLFLD_NOTARRAY=0,判断是否是0下标*/
  684.         s = 0; /*0下标时,没有数组下标起始符#,也没有小标值0的显示*/
  685.     else
  686.         s = snprintf( a, sizeof( a ), "%d", iIndex ) + 1; /*下标的长度+#号码长度*/
  687.     if ( ( l = strlen( cFmlData ) ) + strlen( cFieldName ) + s + strlen( cFieldValue ) + 2 > iBufSize )
  688.         return RC_NSPC; /*判断是否溢出,其中2=域分隔符+'\0'*/
  689.     if ( l > 0 && cFmlData[l-1] != SYM_FML_FS ) /*SYM_FML_FS域分割符^^,原串没有域分割符时*/
  690.     {
  691.         cFmlData[l] = SYM_FML_FS; /*原串尾加上域分隔符*/
  692.         l ++; /*原串长+1*/
  693.     }

  694.     if ( s != 0 ) /*非0下标时*/
  695.         /*a中保存的是下标值*/
  696.         sprintf( cFmlData + l, "%s%c%s%c%s%c", cFieldName, SYM_FML_AN, a, SYM_FML_NV, cFieldValue, SYM_FML_FS );
  697.     else /*0下标时*/
  698.         sprintf( cFmlData + l, "%s%c%s%c", cFieldName, SYM_FML_NV,
  699.         cFieldValue, SYM_FML_FS );

  700.     return RC_SUCC;
  701. }

  702. /*FmlFieldGet() 功能描述: 从总线中取指定域值函数*/
  703. int FmlFieldGet(const char *cFmlData,const char *cFieldName,int    iIndex,    char *cFieldValue,int iValueSize)
  704. {
  705.     char    *p, *q, *v;
  706.     int    l;
  707.     
  708.     if ( ( p = FmlFieldSeek( cFmlData, cFieldName, iIndex ) ) == NULL )
  709.         return RC_NFND; /*p指向指定域名的起始地址*/
  710.     q = strchr( p, SYM_FML_NV ) + 1; /*SYM_FML_NV名值分隔符:,q指向域值的起始地址*/
  711.     if ( ( v = strchr( q, SYM_FML_FS ) ) == NULL ) /*SYM_FML_FS域分隔符^^,v指向域值的尾部地址*/
  712.         l = strlen( q ); /*没有串尾符^^的长度*/
  713.     else
  714.         l = v - q; /*l=域值的长度*/
  715.     if ( l >= iValueSize ) /*要放域值的空间不能小于域值的长度*/
  716.         return RC_NSPC;
  717.     memcpy( cFieldValue, q, l );
  718.     cFieldValue[l] = 0; /*域值的地址加字符串的结束符*/
  719.     return RC_SUCC;
  720. }

  721. /*FmlFieldNum() 功能描述: 统计总线中重复域的个数函数*/
  722. int    FmlFieldNum(const char *cFmlData, const char *cFieldName, int *iNum)
  723. {
  724.     char    *p = (char *) cFmlData;
  725.     char    *n, *q, *v;
  726.     int    r;
  727.     int    num,flag;

  728.     num=0;
  729.     flag=0;

  730.     while ( ( n = strchr( p, SYM_FML_FS ) ) != NULL )
  731.     {
  732.         *n = 0;
  733.         q = strchr( p, SYM_FML_NV );
  734.         *n = SYM_FML_FS;
  735.         if ( q != NULL )
  736.         {
  737.             *q = 0;
  738.             if ( ( v = strchr( p, SYM_FML_AN ) ) != NULL )
  739.                 *v = 0;
  740.             else
  741.             {
  742.                 *iNum=0;
  743.                 if( v != NULL)
  744.                     *v=SYM_FML_AN;
  745.                 *q=SYM_FML_NV;
  746.                 p = n + 1;
  747.                 continue;
  748.             }
  749.             if ( ( r = strcmp( p, cFieldName ) ) == 0 )
  750.             {
  751.                 num++;
  752.                 flag=1;
  753.             }
  754.                 *v=SYM_FML_AN;
  755.                 *q=SYM_FML_NV;
  756.         }
  757.         p = n + 1;
  758.     }
  759.     *iNum=num;
  760.     if(flag == 1)
  761.         return RC_SUCC;
  762.     else
  763.         return RC_NFND;
  764. }

  765. char * StrTrim(char * pstr)
  766. {
  767.     if(NULL == pstr)
  768.     {
  769.         return NULL;
  770.     }
  771.     char * phead = pstr;
  772.     char * ptail = pstr + strlen(pstr) - 1;
  773.     while((*phead == ' ')&&(phead < ptail)) phead++;
  774.     while((*ptail == ' ')&&(ptail > phead)) ptail--;
  775.     *(ptail + 1) = '\0';
  776.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  777.     return pstr;
  778. }

  779. /***********************************************************
  780.  * 函 数: JugeFileExist()
  781.  * 功能描述: 判断文件是否存在
  782.  * 输入参数:lFilename    -    长文件名
  783.  * 返 回:0-文件存在;1-文件不存在
  784.  * 流程描述:
  785.  *    说 明:
  786.  * 修改记录:
  787.  * [修改人] [日期][描述]
  788. ***********************************************************/
  789. int JugeFileExist(char *lFileName)
  790. {
  791.     struct stat statbuf;
  792.     memset(&statbuf,0,sizeof(statbuf));
  793.     stat(lFileName,&statbuf);
  794.     if(statbuf.st_mode & S_IFREG)
  795.         return 0;
  796.     else
  797.         return 1;
  798. }

阅读(882) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~