【C++】標準C++を用いてxml中の情報を簡単に解析する
10749 ワード
複雑なxml解析器に依存する必要のない解析xmlのラベルを簡単に実現した.
1、例えば以下のxml形式のデータ:
2、解析C++コード:
1、例えば以下のxml形式のデータ:
"1.0" encoding="utf-8"?>
MG-APP will be updated automatically
</description>
</descriptions>
"20200529" path= "1"/>
"20200529" path="2"/>
"20200529" path = "3"/>
"20200529" path="MG_APP.exe"/>
"20200529" path="images\splash_logo.jpg"/>
</files>
</update>
2、解析C++コード:
#include
#include
#include
#include
using namespace std;
// Parse one item,such as "ver"
void paraseLabel(const char *ch, const char *label, char *var)
{
char buff[1024] = { 0 };
strcpy_s(buff, ch);
// find ver
char *pStart = NULL, *pEnd = NULL;
pStart = strstr(buff, label);
// find first =
pStart = strstr(pStart, "=");
// find first "
pStart = strstr(pStart, "\"");
pStart += 1;
// find second "
pEnd = strstr(pStart, "\"");
*pEnd = '\0';
// copy ver
strcpy_s(var, strlen(pStart) + 1, pStart);
}
int main()
{
// read file
ifstream getfile;
char buff[1024] = { 0 };
char *p = NULL;
getfile.open("D:/qupdater.xml");
while (!getfile.eof())
{
getfile.getline(buff, 1024);
cout << buff << endl;
p = strstr(buff, ");// find "
if (p) {
char version[512] = { 0 }, filepath[512] = { 0 };
paraseLabel(buff, "ver", version);
paraseLabel(buff, "path", filepath);
cout << version << "->" << filepath << endl;
}
}
getfile.close();
return 0;
}