1, Source code description
Freecplus is a C/C + + open source framework under Linux system. Please go to the C language technology network (www.freecplus.net) to download the source code.
This paper introduces the parsing of string function in xml format with freecplus framework.
The declaration file for functions and classes is freecplus / 65124; freecplus. H.
The definition file for functions and classes is freecplus / 65124; freecplus.cpp.
The sample program is located in the freecplus/demo directory.
The compile rule file is freecplus/demo/makefile.
2, Introduction to xml format string
xml format string is a widely used data format in application development, which is easy to understand, fault-tolerant and extensible. It is the preferred data format for data processing, data communication and data exchange.
The complete xml format is relatively complex, but in the actual development, for our C/C + + programmers, the xml data format used in most scenarios is relatively simple, for example, the xml data set or file content that represents the file list information is 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 set Description:
\< data \ > start of data set.
\< / data \ > end of data set.
\< endl / \ > the end of each row of data.
Filename label: filename.
mtime tag: the time when the file was last modified.
Size label: the size of the file.
3, Analysis of xml format string
In the freecplus framework, a series of functions are provided to parse the following xml format strings.
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: the content of the xml format string to be parsed.
fieldname: the label name of the field.
value: the address of the incoming variable, which is used to store the field content, and supports bool, int, and signed
int, long, unsigned long, double, and char [].
Note: when the data type of the value parameter is char [], the memory of the value array must be sufficient. Otherwise, memory overflow may occur. You can also use the len parameter to limit the length of the field content. The default value of len is 0, which means the length of the field content is not limited.
Return value: true - get success; false - get failure.
Example (demo22.cpp)
/* * Program name: demo22.cpp, this program demonstrates calling GetXMLBuffer function of freecplus framework to parse xml string. * Author: C language technology network (www.freecplus.net) date: 20190525 */ #include "../_freecplus.h" // A structure used to store information about football players. struct st_player { char name[51]; // Full name char no[6]; // uniform number bool striker; // Whether the position on the field is forward, true - yes; false - No. int age; // Age double weight; // Weight, kg. long sal; // Annual salary, euro. char club[51]; // Club in effect }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); // Output results: name=messi,no=10,striker=1,age=30,weight=68.5,sal=21000000,club=Barcelona }
4, Application experience
For C/C + + programmers, using simple xml strings to express data can improve development efficiency. I don't recommend using complex xml format, which will make the program code very annoying.
If you need to parse more complex xml in actual development, you can find open source libraries on the Internet, such as libxml + +.
5, Copyright notice
C language technology net original article, reprint please explain the article source, the author and the original link.
Source: C language technology network (www.freecplus.net)
Author: Manon Youdao
If this article is helpful to you, please like support or forward my article in your blog, thank you!!!
If there is a mistake in the article, or there is a mistake in the content, or other suggestions and opinions, please leave a message for correction, thank you very much!!!