//unicode to UT8
std::string to_utf8(const wchar_t* buffer, int len)
{
int nChars = ::WideCharToMultiByte(
CP_UTF8,
0,
buffer,
len,
NULL,
0,
NULL,
NULL);
if (nChars == 0) return \"\";
std::string newbuffer;
newbuffer.resize(nChars);
::WideCharToMultiByte(
CP_UTF8,
0,
buffer,
len,
const_cast<char*>(newbuffer.c_str()),
nChars,
NULL,
NULL);
return newbuffer;
}
std::string to_utf8(const std::wstring& str)
{
return to_utf8(str.c_str(), (int)str.size());
}
BOOL StringToWString(const std::string &str, std::wstring &wstr)
{
int nLen = (int)str.length();
wstr.resize(nLen, L\' \');
int nResult = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str.c_str(), nLen, (LPWSTR)wstr.c_str(), nLen);
if (nResult == 0)
{
return FALSE;
}
return TRUE;
}
//把UTF-8数据转成unicode
int Utf82Unicode(const char* utf, wchar_t* unicode, int nBuffSize)
{
if (!utf || !strlen(utf))
{
return 0;
}
int dwUnicodeLen = MultiByteToWideChar(CP_UTF8, 0, utf, -1, NULL, 0);
size_t num = dwUnicodeLen*sizeof(wchar_t);
if (num > nBuffSize)
{
return 0;
}
MultiByteToWideChar(CP_UTF8, 0, utf, -1, unicode, dwUnicodeLen);
return dwUnicodeLen;
}
完整的代码如下:
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//rapid
#include \"stdafx.h\"
#include \"rapid .hpp\"
#include \"rapid _utils.hpp\" //rapid ::file
#include \"rapid _print.hpp\" //rapid ::print
#include <windows.h>
#include <iostream>
std::string to_utf8(const wchar_t* buffer, int len)
{
int nChars = ::WideCharToMultiByte(
CP_UTF8,
0,
buffer,
len,
NULL,
0,
NULL,
NULL);
if (nChars == 0) return \"\";
std::string newbuffer;
newbuffer.resize(nChars);
::WideCharToMultiByte(
CP_UTF8,
0,
buffer,
len,
const_cast<char*>(newbuffer.c_str()),
nChars,
NULL,
NULL);
return newbuffer;
}
std::string to_utf8(const std::wstring& str)
{
return to_utf8(str.c_str(), (int)str.size());
}
BOOL StringToWString(const std::string &str, std::wstring &wstr)
{
int nLen = (int)str.length();
wstr.resize(nLen, L\' \');
int nResult = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str.c_str(), nLen, (LPWSTR)wstr.c_str(), nLen);
if (nResult == 0)
{
return FALSE;
}
return TRUE;
}
int Utf82Unicode(const char* utf, wchar_t* unicode, int nBuffSize)
{
if (!utf || !strlen(utf))
{
return 0;
}
int dwUnicodeLen = MultiByteToWideChar(CP_UTF8, 0, utf, -1, NULL, 0);
size_t num = dwUnicodeLen*sizeof(wchar_t);
if (num > nBuffSize)
{
return 0;
}
MultiByteToWideChar(CP_UTF8, 0, utf, -1, unicode, dwUnicodeLen);
return dwUnicodeLen;
}
void createTest(const char * fileName)
{
rapid :: _document<> doc;
rapid :: _node<> *delcartion = doc.allocate_node(rapid ::node_declaration);
rapid :: _attribute<> *ver = doc.allocate_attribute(\"version\", \"1.0\");
rapid :: _attribute<> *encode = doc.allocate_attribute(\"encoding\", \"utf-8\");
delcartion->append_attribute(ver);
delcartion->append_attribute(encode);
doc.append_node(delcartion);
rapid :: _node<> *root = doc.allocate_node(rapid ::node_element, \"person\");
root->append_attribute(doc.allocate_attribute(\"id\", \"KD0153\"));
root->append_attribute(doc.allocate_attribute(\"name\", \"张三\"));
rapid :: _node<> *idNode = doc.allocate_node(rapid ::node_element, \"home\");
idNode->append_attribute(doc.allocate_attribute(\"id\", \"111\"));
rapid :: _node<> *addrNode = doc.allocate_node(rapid ::node_element, \"addr\");
addrNode->append_attribute(doc.allocate_attribute(\"addr\", \"威尼斯\"));
root->append_node(idNode);
root->append_node(addrNode);
doc.append_node(root);
std::string text;
rapid ::print(std::back_inserter(text), doc, 0);
std::wstring wInfoText;
StringToWString(text, wInfoText);
std::ofstream inf(fileName, std::ios::out);
inf << to_utf8(wInfoText);
}
void create(const char * file_name)
{
char buf[1024] = { 0 };
rapid :: _document<> doc;
// 声明
rapid :: _node<>* declaration = doc.allocate_node(rapid ::node_declaration);
declaration->append_attribute(doc.allocate_attribute(\"version\", \"1.0\"));
declaration->append_attribute(doc.allocate_attribute(\"encoding\", \"utf-8\"));
doc.append_node(declaration);
// 根节点
rapid :: _node<>* root = doc.allocate_node(rapid ::node_element, \"root\");
doc.append_node(root);
// 注释节点1
rapid :: _node<>* comment1 = doc.allocate_node(rapid ::node_comment, 0, \"all students info\");
root->append_node(comment1);
// 普通节点1
rapid :: _node<>* students = doc.allocate_node(rapid ::node_element, \"students\");
for (int i = 0; i < 10; ++i)
{
rapid :: _node<>* one_student = doc.allocate_node(rapid ::node_element, \"student\");
//sprintf_s(buf, \"student_%02d\", i);
// doc.allocate_string 的作用是将源字符串深拷贝一份
one_student->append_attribute(doc.allocate_attribute(\"name\", doc.allocate_string(buf)));
one_student->append_attribute(doc.allocate_attribute(\"score\", doc.allocate_string(std::to_string(100 - i).c_str())));
students->append_node(one_student);
}
root->append_node(students);
// 注释节点2
rapid :: _node<>* comment2 = doc.allocate_node(rapid ::node_comment, 0, \"all books info\");
root->append_node(comment2);
// 普通节点2
rapid :: _node<>* books = doc.allocate_node(rapid ::node_element, \"books\");
for (int i = 0; i < 10; ++i)
{
rapid :: _node<>* one_book = doc.allocate_node(rapid ::node_element, \"book\");
//sprintf_s(buf, \"book_%02d\", i);
// doc.allocate_string 的作用是将源字符串深拷贝一份
one_book->append_attribute(doc.allocate_attribute(\"name\", doc.allocate_string(buf)));
one_book->append_attribute(doc.allocate_attribute(\"price\", doc.allocate_string(std::to_string(50 - i).c_str())));
books->append_node(one_book);
}
root->append_node(books);
std::ofstream outfile(file_name, std::ios::out);
if (outfile)
{
std::string text;
rapid ::print(std::back_inserter(text), doc, 0);
/*char *end = rapid ::print(buf, doc, 0);
*end = 0;*/
outfile << text;
outfile.close();
}
}
void parseTest2(const char * fileName)
{
std::ifstream inf(fileName, std::ios::in);
if (!inf)
{
return;
}
std::string sStr;
while (inf)
{
std::string sLine;
std::getline(inf, sLine);
sStr += sLine;
}
int nLen = strlen(sStr.c_str());
wchar_t *pContent = new wchar_t[nLen + 1];
ZeroMemory(pContent, nLen + 1);
Utf82Unicode(sStr.c_str(), pContent, nLen*sizeof(wchar_t));
}
void parseTest(const char * fileName)
{
std::ifstream inf(fileName, std::ios::in);
if (!inf)
{
return;
}
inf.seekg(0, std::ios::end);
int nLen = inf.tellg();
inf.seekg(0, std::ios::beg);
char * strc = new char[nLen+1];
ZeroMemory(strc, nLen + 1);
inf.read(strc, nLen);
rapid :: _document<> doc;
doc.parse<0>(strc);
rapid :: _node<> *root = doc.first_node(\"root\");
int nSize = 0;
rapid :: _node<>* child = root->first_node(\"students\")->first_node();
while (child)
{
//判断属性是否存在
rapid :: _attribute<>* nameAttr = child->first_attribute(\"name\");
rapid :: _attribute<>* scoreAttr = child->first_attribute(\"score\");
char *nameC;
char *scoreC;
if (nameAttr)
{
nameC = nameAttr->value();
}
if (scoreAttr)
{
scoreC = scoreAttr->value();
}
child = child->next_sibling();
}
/*for (rapid :: _node<>* child = root->first_node(\"students\")->first_node(); child; child = child->next_sibling())
{
char *nameC = child->first_attribute(\"name\")->value();
char * homea = child->first_attribute(\"score\")->value();
}*/
delete strc;
strc = NULL;
}
/*
void parse(const char * file_name)
{
std::ifstream infile(file_name, std::ios::in);
if (!infile)
{
return;
}
infile.seekg(0, std::ios::end);
int nLen = infile.tellg();
infile.seekg(0, std::ios::beg);
char * strc = new char[nLen + 1];
ZeroMemory(strc, nLen + 1);
infile.read(strc, nLen);
rapid :: _document<> doc;
doc.parse<0>(strc);
// 取得根节点
rapid :: _node<> *root = doc.first_node(\"root\");
// 遍历students的子节点
for (rapid :: _node<> * node = root->first_node(\"students\")->first_node(); node; node = node->next_sibling())
{
char *name2 = node->first_attribute(\"name\")->value();
char *score2 = node->first_attribute(\"score\")->value();
}
// 遍历books的子节点
for (rapid :: _node<> * node = root->first_node(\"books\")->first_node(); node; node = node->next_sibling())
{
char * vl = node->first_attribute(\"name\")->value();
char *v2 = node->first_attribute(\"price\")->value();
}
delete strc;
strc = NULL;
}
*/
int _tmain(int argc, _TCHAR* argv[])
{
//create(\"11. \");
createTest(\"22. \");
parseTest2(\"22. \");
parseTest(\"22. \");
return 0;
}
继续阅读与本文标签相同的文章
Android App启动流程和启动模式
-
第六届世界互联网大会即将举行 乌镇将试行5G自动微公交
2026-05-18栏目: 教程
-
2019全球智能化商业峰会秋季场下周三开幕
2026-05-18栏目: 教程
-
电梯间的“刷屏王”,全力创新抢占商业市场
2026-05-18栏目: 教程
-
大宗货运如何实现“重去重回”
2026-05-18栏目: 教程
-
世界互联网大会就在这里 互联网之光博览中心抢先看
2026-05-18栏目: 教程
