1.修改xml文件:
bool write(char *file_path, std::vector &edit)
{
//读取xml文件中的参数值
TiXmlDocument* Document = new TiXmlDocument();
if (!Document->LoadFile(FILE_PATH))
{
return false;
}
TiXmlElement* RootElement = Document->RootElement(); //根目录,这里是
TiXmlElement* NextElement = RootElement->FirstChildElement(); //根目录下的第一个节点层 这里是
if (NextElement->ValueTStr() == "Edits") {
TiXmlElement* BoxElement = NextElement->FirstChildElement();//这里是str_num
{
int i = 0;
while (BoxElement != NULL) {
BoxElement->Clear();//将原有的文本删除
TiXmlText*pValue = new TiXmlText(edit[i].c_str());//得到新的文本
BoxElement->LinkEndChild(pValue);//更改文本
Document->SaveFile(FILE_PATH);//保存文本
i++;
BoxElement = BoxElement->NextSiblingElement();
}
}
}
return true;
}
保存中文字符, 只需要将字符串转为utf-8的格式即可。
char* G2U(const char* gb2312)
{
int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
if (wstr) delete[] wstr;
return str;
}
完整的代码如下:
#include "stdafx.h"
#include"tinyxml.h"
#include"tinystr.h"
#include
#include
#include
#include
#define FILE_PATH "F:\\visual studio 2015\\xml的读取和修改以及中文字符的存储\\test.xml"
bool read(char *file_path,std::vector &edit)
{
//读取xml文件中的参数值
TiXmlDocument* Document = new TiXmlDocument();
if (!Document->LoadFile(FILE_PATH))
{
return false;
}
TiXmlElement* RootElement = Document->RootElement(); //根目录,这里是
TiXmlElement* NextElement = RootElement->FirstChildElement(); //根目录下的第一个节点层 这里是
if (NextElement->ValueTStr() == "Edits") {
TiXmlElement* BoxElement = NextElement->FirstChildElement();//这里是str_num
{
while (BoxElement != NULL) {
edit.push_back(BoxElement->GetText());//获取BoxElement中的字符串
BoxElement = BoxElement->NextSiblingElement();//获取BoxElement的下一个节点
}
}
}
return true;
}
char* G2U(const char* gb2312)
{
int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
if (wstr) delete[] wstr;
return str;
}
bool write(char *file_path, std::vector &edit)
{
//读取xml文件中的参数值
TiXmlDocument* Document = new TiXmlDocument();
if (!Document->LoadFile(FILE_PATH))
{
return false;
}
TiXmlElement* RootElement = Document->RootElement(); //根目录,这里是
TiXmlElement* NextElement = RootElement->FirstChildElement(); //根目录下的第一个节点层 这里是
if (NextElement->ValueTStr() == "Edits") {
TiXmlElement* BoxElement = NextElement->FirstChildElement();//这里是str_num
{
int i = 0;
while (BoxElement != NULL) {
BoxElement->Clear();//将原有的文本删除
const char *a = edit[i].c_str();
char *c = G2U(a);//为了保存中文字符需要将字符转为utf8
TiXmlText*pValue = new TiXmlText(c);//得到新的文本
BoxElement->LinkEndChild(pValue);//更改文本
Document->SaveFile(FILE_PATH);//保存文本
i++;
BoxElement = BoxElement->NextSiblingElement();
}
}
}
return true;
}
int main()
{
std::vector edit;
bool c = read(FILE_PATH, edit);
std::cout << edit.size() << std::endl;
edit[0] = "301";
edit[1] = "103";
edit[2] = "正常";
edit[3] = "%90";
edit[4] = "NG";
c = write(FILE_PATH, edit);
std::cout << c << std::endl;
system("pause");
return 0;
}
得到的xml结果:
301
103
正常
%90
NG