关于Tinyxml的故事就不多说了,本文主要讲怎么利用它创建xml文档 以 xml string形式在内存中解析xml ,以及关于UTF-8编码格式的XML文件如何写入、显示中文字符串等细节问题。
下载Tinyxml Address:http://www.grinninglizard.com/tinyxml/
文档详解Address:http://www.grinninglizard.com/tinyxmldocs/index.html
解压后采用VS2010进行编译,生成 静态库文件–tinyxml.lib。创建工程并将下图六个文件添加到工程中,同时将tinyxml.lib文件添加到工程(也可以直接copy到工程目录下,采用#pragma 语句进行包含连接,本文就是这种方式。):
接下来就是code:
#include "tinyxml.h"
#include "tinystr.h"
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
#pragma comment(lib,"tinyxml.lib")
// 中文字符转UTF-8
void MBSToUTF8(char * utf8, int size, const char* mbs)
{
if (!utf8 || !mbs)
return ;
UINT nACP = GetACP();
int dwNum = MultiByteToWideChar(nACP, 0, mbs, -1, NULL, 0); // CP_ACP
if (dwNum <= 0)
return ;
wchar_t* pwText = NULL;
pwText = new wchar_t[dwNum + 1];
if (!pwText)
{
delete[] pwText;
return ;
}
memset(pwText, 0, dwNum * 2 + 2);
if (MultiByteToWideChar(nACP, 0, mbs, -1, pwText, dwNum) == 0) // CP_ACP
{
delete[] pwText;
return ;
}
int dwCount = WideCharToMultiByte(CP_UTF8, 0, pwText, -1, NULL, 0, NULL, NULL);
if (dwCount <= 0)
{
delete[] pwText;
return ;
}
if (size < dwCount + 1)
{
delete[] pwText;
return ;
}
memset(utf8, 0, dwCount + 1);
if (WideCharToMultiByte(CP_UTF8, 0, pwText, -1, utf8, dwCount, NULL, NULL) == 0)
{
delete[] pwText;
return ;
}
delete[] pwText;
}
//UTF-8转中文字符
void UTF8ToMBS(char * mbs, int size, const char* utf8)
{
if (!utf8 || !mbs)
return ;
int dwNum = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
if (dwNum <= 0)
return ;
wchar_t* pwText = NULL;
pwText = new wchar_t[dwNum + 1];
if (!pwText)
{
delete[] pwText;
return ;
}
memset(pwText, 0, dwNum * 2 + 2);
if (MultiByteToWideChar(CP_UTF8, 0, utf8, -1, pwText, dwNum) == 0)
{
delete[] pwText;
return ;
}
UINT nACP = GetACP();
int dwCount = WideCharToMultiByte(nACP, 0, pwText, -1, NULL, 0, NULL, NULL); // CP_ACP
if (dwCount <= 0)
{
delete[] pwText;
return ;
}
if (size < dwCount + 1)
{
delete[] pwText;
return ;
}
memset(mbs, 0, dwCount + 1);
if (WideCharToMultiByte(nACP, 0, pwText, -1, mbs, dwCount, NULL, NULL) == 0) // CP_ACP
{
delete[] pwText;
return ;
}
delete[] pwText;
}
//创建xml
string CreateXMLFile()
{
TiXmlPrinter printer;
string xmlstr;
char utf8[32] = { 0 };
MBSToUTF8(utf8, sizeof(utf8), "周星星");
// 创建一个XML的文档对象。
TiXmlDocument *xmlDocument = new TiXmlDocument();
TiXmlDeclaration * decl = new TiXmlDeclaration("1.0", "UTF-8", "");
xmlDocument->LinkEndChild(decl);
// 根元素。
TiXmlElement *rootElement = new TiXmlElement("ROOT");
// 连接到文档对象, 作为根元素
xmlDocument->LinkEndChild(rootElement);
// 创建一个Book元素.
TiXmlElement *RegisterElement = new TiXmlElement("REGISTER");
// 连接到根元素, 就是根元素的子元素
rootElement->LinkEndChild(RegisterElement);
// 创建Name元素和ID元素
TiXmlElement *numberElement = new TiXmlElement("USER_ID");
TiXmlElement *nameElement = new TiXmlElement("USER_NAME");
// 连接
RegisterElement->LinkEndChild(numberElement);
RegisterElement->LinkEndChild(nameElement);
// 设置Name元素和Price元素的值。
TiXmlText *numberValue = new TiXmlText("12345");
numberElement->LinkEndChild(numberValue);
TiXmlText *nameValue = new TiXmlText(utf8);
nameElement->LinkEndChild(nameValue);
xmlDocument->Accept(&printer);
xmlstr = printer.CStr();
return xmlstr;
}
int main(int argc, char* argv[])
{
// 创建xml 并以string形式保存
string xmlstr = CreateXMLFile();
cout << xmlstr;
//解析xml string
const char *content = xmlstr.c_str();
TiXmlDocument *doc = new TiXmlDocument();
doc->Parse(content);
if (&doc == NULL)
cout << "doc == NULL" << endl;
TiXmlHandle docHandle(doc);
TiXmlNode * Point_name= docHandle.FirstChild("ROOT").FirstChild("REGISTER").FirstChild("USER_NAME").ToElement();
TiXmlElement * elemElem = Point_name->ToElement();
const char *utf = elemElem->GetText();
//处理xml中的中文,确保其正确显示
char chinese[32] = { 0 };
UTF8ToMBS(chinese, sizeof(chinese), utf);
cout << chinese << endl;
return 0;
}
// 以文件的形式保存xml文件
/*bool CreateXMLFile(std::string& strFileName)
{
try
{
// 创建一个XML的文档对象。
TiXmlDocument *xmlDocument = new TiXmlDocument();
// 根元素。
TiXmlElement *rootElement = new TiXmlElement("Books");
// 连接到文档对象, 作为根元素
xmlDocument->LinkEndChild(rootElement);
// 创建一个Book元素.
TiXmlElement *bookElement = new TiXmlElement("Book");
// 连接到根元素, 就是根元素的子元素
rootElement->LinkEndChild(bookElement);
// 设置Book元素的属性, 这里是ID。
bookElement->SetAttribute("ID", "1");
// 创建Name元素和Price元素
TiXmlElement *nameElement = new TiXmlElement("Name");
TiXmlElement *priceElement = new TiXmlElement("Price");
// 连接
bookElement->LinkEndChild(nameElement);
bookElement->LinkEndChild(priceElement);
// 设置Name元素和Price元素的值。
TiXmlText *nameValue = new TiXmlText("葵花宝典");
nameElement->LinkEndChild(nameValue);
TiXmlText *priceValue = new TiXmlText("50.00");
priceElement->LinkEndChild(priceValue);
xmlDocument->SaveFile(strFileName.c_str()); // 保存到文件
}
catch (...)
{
return false;
}
return true;
}*/
// 以文件的形式加载 解析
/*bool ReadXMLFile(std::string& szFileName)
{
try
{
// 创建一个XML的文档对象。
TiXmlDocument *xmlDocument = new TiXmlDocument(szFileName.c_str());
// 解析
xmlDocument->LoadFile();
// 获得根元素
TiXmlElement *rootElement = xmlDocument->RootElement();
// 输出根元素名称
std::cout << rootElement->Value() << std::endl;
// 获得第一个节点。
TiXmlElement *firstElement = rootElement->FirstChildElement();
// 获得第一个Person的name节点和age节点和ID属性。
TiXmlElement *nameElement = firstElement->FirstChildElement();
TiXmlElement *priceElement = nameElement->NextSiblingElement();
TiXmlAttribute *IDAttribute = firstElement->FirstAttribute();
// 输出
std::cout << nameElement->FirstChild()->Value() << std::endl;
std::cout << priceElement->FirstChild()->Value() << std::endl;
std::cout << IDAttribute->Value() << std::endl;
}
catch (...)
{
return false;
}
return true;
}*/
处理结果:
关于结果的几点解释:
1、xml 中的中文在window 窗口显示为乱码,完全不用惊讶,这个与数据编码和默认打开方式有关。如果你将该xml以UTF-8形式保存,并以UTF-8形式打开,完全可以正确显示。
2、如果你将xml中 中文部分的编码不进行UTF8ToMBS转码,显示就会如1中所说的乱码一样。