示例xml:
<?xml version="1.0"?>
<grades>
<grade>
<id>2019001</id>
<name>张三</name>
<course>机器学习</course>
<score>85</score>
</grade>
</grades>
main():
CfstreamXML ctrl;
ctrl.Open("c:/1/1.xml");
string name, course, score;
char id[10];
ctrl.ReadWith("<id>", "</id>", id);
ctrl.ReadWith("<name>", "</name>", name);
ctrl.ReadWith("<course>", "</course>", course);
ctrl.ReadWith("<score>", "</score>", score);
ctrl.Close();
cout << id << ',' << name << ',' << course << ',' << score << endl;
结果:
2019001,张三,机器学习,85
.h头文件:
#pragma once
#include <fstream>
class CfstreamXML
{
public:
CfstreamXML();
void Open(const std::string& xml_adress);
void Open(const char* xml_adress);
void Close();
void ReadWith(const char* char_begin, const char* char_end, char* Readinfo);
void ReadWith(const char* char_begin, const char* char_end, std::string& Readinfo_str);
bool GetEOF();
bool IsNew();
private:
std::ifstream m_ifs;
bool m_EOF;
bool m_NewFlag;
};
.cpp实现文件
#include "CfstreamXML.h"
CfstreamXML::CfstreamXML()
: m_EOF(false)
, m_NewFlag(false)
{
}
void CfstreamXML::Open(const std::string& xml_adress)
{
m_ifs.open(xml_adress);
}
void CfstreamXML::Open(const char* xml_adress)
{
m_ifs.open(xml_adress);
}
void CfstreamXML::Close()
{
m_ifs.close();
}
void CfstreamXML::ReadWith(const char* char_begin, const char* char_end, char* Readinfo)
{
m_NewFlag = false;
int beginsize = strlen(char_begin);
int endsize = strlen(char_end);
char temp[1024];
while (!m_ifs.eof()) {
//非空则读一行
m_ifs.getline(temp, sizeof(temp));
//简单字符串比较,i为Readindo字段开始索引,j为Readinfo字段末尾下一个索引, k为子字符串比较索引
//寻找开始字符串
int i, j, k;
for (i = 0, k = 0; temp[i] != '\0' && k < beginsize; i++) {
if (temp[i] == char_begin[k]) {
k++;
}
else {
i = i - k;
k = 0;
}
}
//当前行未找到匹配起始字符串则结束本轮while开始读下一行
if (k < beginsize)
continue;
//寻找结束字符串
for (j = i, k = 0; temp[j] != '\0' && k < endsize; j++) {
if (temp[j] == char_end[k]) {
k++;
}
else {
j = j - k;
k = 0;
}
}
//回到匹配结尾字符串的位置,即j为Readinfo字段末尾下一个索引
j = j - k;
//复制Readinfo
for (k = 0; k < j - i; k++) {
Readinfo[k] = temp[i + k];
}
Readinfo[k] = '\0';
m_NewFlag = true;
break;
}
if (m_ifs.eof())
m_EOF = true;
}
void CfstreamXML::ReadWith(const char* char_begin, const char* char_end, std::string& Readinfo_str)
{
m_NewFlag = false;
char Readinfo[1024];
int beginsize = strlen(char_begin);
int endsize = strlen(char_end);
char temp[1024];
while (!m_ifs.eof()) {
//非空则读一行
m_ifs.getline(temp, sizeof(temp));
//简单字符串比较,i为Readindo字段开始索引,j为Readinfo字段末尾下一个索引, k为子字符串比较索引
//寻找开始字符串
int i, j, k;
for (i = 0, k = 0; temp[i] != '\0' && k < beginsize; i++) {
if (temp[i] == char_begin[k]) {
k++;
}
else {
i = i - k;
k = 0;
}
}
//当前行未找到匹配起始字符串则结束本轮while开始读下一行
if (k < beginsize)
continue;
//寻找结束字符串
for (j = i, k = 0; temp[j] != '\0' && k < endsize; j++) {
if (temp[j] == char_end[k]) {
k++;
}
else {
j = j - k;
k = 0;
}
}
//回到匹配结尾字符串的位置,即j为Readinfo字段末尾下一个索引
j = j - k;
//复制Readinfo
for (k = 0; k < j - i; k++) {
Readinfo[k] = temp[i + k];
}
Readinfo[k] = '\0';
Readinfo_str = Readinfo;
m_NewFlag = true;
break;
}
if (m_ifs.eof())
m_EOF = true;
}
bool CfstreamXML::GetEOF()
{
return m_EOF;
}
bool CfstreamXML::IsNew()
{
return m_NewFlag;
}