RapidXML 以Utf-8文件保存及读取

//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 : 定义控制台应用程序的入口点。
//rapidXML

#include "stdafx.h"

扫描二维码关注公众号,回复: 4965247 查看本文章

#include "rapidxml.hpp"
#include "rapidxml_utils.hpp"  //rapidxml::file
#include "rapidxml_print.hpp"  //rapidxml::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)
{
    rapidxml::xml_document<> doc;
    rapidxml::xml_node<> *delcartion = doc.allocate_node(rapidxml::node_declaration);
    rapidxml::xml_attribute<> *ver = doc.allocate_attribute("version", "1.0");
    rapidxml::xml_attribute<> *encode = doc.allocate_attribute("encoding", "utf-8");
    delcartion->append_attribute(ver);
    delcartion->append_attribute(encode);
    doc.append_node(delcartion);
    rapidxml::xml_node<> *root =  doc.allocate_node(rapidxml::node_element, "person");
    root->append_attribute(doc.allocate_attribute("id", "KD0153"));
    root->append_attribute(doc.allocate_attribute("name", "张三"));
    rapidxml::xml_node<> *idNode = doc.allocate_node(rapidxml::node_element, "home");
    idNode->append_attribute(doc.allocate_attribute("id", "111"));
    rapidxml::xml_node<> *addrNode = doc.allocate_node(rapidxml::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;
    rapidxml::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 };
    rapidxml::xml_document<> doc;
    // 声明
    rapidxml::xml_node<>* declaration = doc.allocate_node(rapidxml::node_declaration);
    declaration->append_attribute(doc.allocate_attribute("version", "1.0"));
    declaration->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
    doc.append_node(declaration);

    // 根节点
    rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "root");
    doc.append_node(root);

    // 注释节点1
    rapidxml::xml_node<>* comment1 = doc.allocate_node(rapidxml::node_comment, 0, "all students info");
    root->append_node(comment1);

    // 普通节点1
    rapidxml::xml_node<>* students = doc.allocate_node(rapidxml::node_element, "students");
    for (int i = 0; i < 10; ++i)
    {
        rapidxml::xml_node<>* one_student = doc.allocate_node(rapidxml::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
    rapidxml::xml_node<>* comment2 = doc.allocate_node(rapidxml::node_comment, 0, "all books info");
    root->append_node(comment2);
    // 普通节点2
    rapidxml::xml_node<>* books = doc.allocate_node(rapidxml::node_element, "books");
    for (int i = 0; i < 10; ++i)
    {
        rapidxml::xml_node<>* one_book = doc.allocate_node(rapidxml::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;
        rapidxml::print(std::back_inserter(text), doc, 0);

        /*char *end = rapidxml::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);
    rapidxml::xml_document<> doc;
    doc.parse<0>(strc);
    rapidxml::xml_node<> *root = doc.first_node("root");
    int nSize = 0;
    
    rapidxml::xml_node<>* child = root->first_node("students")->first_node();
    while (child)
    {
        //判断属性是否存在
        rapidxml::xml_attribute<>* nameAttr = child->first_attribute("name");
        rapidxml::xml_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 (rapidxml::xml_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);

    rapidxml::xml_document<> doc;
    doc.parse<0>(strc);
    // 取得根节点
    rapidxml::xml_node<> *root = doc.first_node("root");
    // 遍历students的子节点
    for (rapidxml::xml_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 (rapidxml::xml_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.xml");
    createTest("22.xml");
    parseTest2("22.xml");
    parseTest("22.xml");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_16628589/article/details/85063480
今日推荐