freecplus analytical framework -xml

A source code description

freecplus is a C / C ++ open source framework under a Linux system, go to the C language source code Technology Network (www.freecplus.net) download.

This article describes the use of analytic xml freecplus frame format string functions.

Declaration file functions and classes are freecplus / _freecplus.h.

Definition file functions and classes are freecplus / _freecplus.cpp.

Sample program located freecplus / demo directory.

Compiled rules file is freecplus / demo / makefile.

Two, xml format string description

xml format string is a data application format is widely used in the development of easy to understand, fault tolerance and scalability is very good, the data is processed, the preferred data format of scenario data communication and data exchange.

Complete xml format is more complex, however, in the actual development of our C / C ++ programmers, used under most of the scenarios xml data format is simple, for example, indicates that the file list information of xml data set or file content as follows:

<data>
<filename>_freecplus.h</filename><mtime>2020-01-01 12:20:35</mtime><size>1834</size><endl/>
<filename>_freecplus.cpp</filename><mtime>2020-01-01 10:10:15</mtime><size>5094</size><endl/>
</data>

Data sets Description:

<Data>: data set begins.

</ Data>: data set ended.

<Endl />: the end of each row of data.

filename tag: file name.

mtime tags: file was last modified.

Label size: the size of the file.

Third, the analytic xml format string

In freecplus framework provides the following function parses a xml-based format string.

Function declaration:

bool GetXMLBuffer(const char *xmlbuffer,const char *fieldname,bool   *value);
bool GetXMLBuffer(const char *xmlbuffer,const char *fieldname,int    *value);
bool GetXMLBuffer(const char *xmlbuffer,const char *fieldname,unsigned int *value);
bool GetXMLBuffer(const char *xmlbuffer,const char *fieldname,long   *value);
bool GetXMLBuffer(const char *xmlbuffer,const char *fieldname,unsigned long *value);
bool GetXMLBuffer(const char *xmlbuffer,const char *fieldname,double *value);
bool GetXMLBuffer(const char *xmlbuffer,const char *fieldname,char *value,const int ilen=0);

Parameter Description:

xmlbuffer: xml Content format string to be parsed.

fieldname: tag name field.

value: Incoming address of a variable, used to store the contents of the field, support BOOL, int, insigned
int, Long, unsigned Long, Double and char [].

Note that, when the data type of the value parameter is char [], the value of the array must ensure that enough memory, or memory overflow problem may occur may be limited by the contents of the length field acquisition parameters ILEN, ILEN default value 0, represent field contents acquired is not limited length.

Return value: true- succeed; false- acquisition failure.

Example (demo22.cpp)

/*
 *  程序名:demo22.cpp,此程序演示调用freecplus框架的GetXMLBuffer函数解析xml字符串。
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include "../_freecplus.h"

// 用于存放足球运动员资料的结构体。
struct st_player
{
  char name[51];    // 姓名
  char no[6];       // 球衣号码
  bool striker;     // 场上位置是否是前锋,true-是;false-不是。
  int  age;         // 年龄
  double weight;    // 体重,kg。
  long sal;         // 年薪,欧元。
  char club[51];    // 效力的俱乐部
}stplayer;

int main()
{
  memset(&stplayer,0,sizeof(struct st_player));

  char buffer[301];  
  STRCPY(buffer,sizeof(buffer),"<name>messi</name><no>10</no><striker>true</striker><age>30</age><weight>68.5</weight><sal>21000000</sal><club>Barcelona</club>");

  GetXMLBuffer(buffer,"name",stplayer.name,50);
  GetXMLBuffer(buffer,"no",stplayer.no,5);
  GetXMLBuffer(buffer,"striker",&stplayer.striker);
  GetXMLBuffer(buffer,"age",&stplayer.age);
  GetXMLBuffer(buffer,"weight",&stplayer.weight);
  GetXMLBuffer(buffer,"sal",&stplayer.sal);
  GetXMLBuffer(buffer,"club",stplayer.club,50);

  printf("name=%s,no=%s,striker=%d,age=%d,weight=%.1f,sal=%ld,club=%s\n",\
         stplayer.name,stplayer.no,stplayer.striker,stplayer.age,\
         stplayer.weight,stplayer.sal,stplayer.club);
  // 输出结果:name=messi,no=10,striker=1,age=30,weight=68.5,sal=21000000,club=Barcelona
}

Fourth, the application experience

For C / C ++ programmers, the simple xml string expression data can improve development efficiency, I do not recommend the use of complex xml format, the program will lock code is annoying.

If you need to resolve more complex xml in the actual development, you can find online open source libraries, such as libxml ++.

V. Copyright

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published 159 original articles · won praise 458 · views 120 000 +

Guess you like

Origin blog.csdn.net/wucz122140729/article/details/105187169