CMakeup解析中文乱码及不同版本的区别

有空再来细说,先贴个代码占位置
#include <iostream>
#include <string>
#include <cstdlib>
#include <tchar.h>
#include <stdio.h>
#include <Windows.h>
/////////////////////////////////////////////////////
//desc:
//测试文件:test.xml,文件编码格式:utf-8
//旧版本:CMarkupSTL7.1,新版本:CMarkup:11.5
//开启 预定义 OLD_MARKUP_VERSION ,xml的解析使用旧的CMarkupSTL
//注释掉该行,则xml的解析使用新的CMarkup
//并且可以通过更改项目属性中的字符集,来确定该如何解析
/////////////////////////////////////////////////////
#define OLD_MARKUP_VERSION 1

#ifdef OLD_MARKUP_VERSION 
#include "markupstl.h"
#define  CMarkup CMarkupSTL
#else
#include "Markup.h"
#endif

int main(void)
{
	CMarkup m_MarkupSTL;

#if (defined(MARKUP_WCHAR)   && ! defined(OLD_MARKUP_VERSION) )

	std::cout << "UNICODE MODEL" << std::endl;

	if(!m_MarkupSTL.Load(_T("test.xml"))) 
	{
		std::cout<<"the path error!";
		return false;
	}

	m_MarkupSTL.ResetPos();

	while ( m_MarkupSTL.FindChildElem(_T("pp_data")))
	{
		std::wstring docId=m_MarkupSTL.GetChildAttrib(_T("id"));
		//docId=atoi(m_MarkupSTL.GetChildAttrib("id").c_str());
		std::wcout<<"movie id:"<< docId << std::endl;
		m_MarkupSTL.IntoElem();                ///attention here!
		if(m_MarkupSTL.FindChildElem(_T("url"))) //字段读取顺序和xml文件要一致
		{
			std::wstring url=m_MarkupSTL.GetChildData();
			if(url.empty() || url[0]=='\0')
				url = _T("12345");
			std::wcout << L"url :" << url << std::endl;
		}

		

		m_MarkupSTL.FindChildElem(_T("img"));
		std::wcout << m_MarkupSTL.GetChildData() << std::endl;

		//
		char ctf[1000];

		m_MarkupSTL.FindChildElem(_T("name"));
		std::wstring name = m_MarkupSTL.GetChildData();

		WideCharToMultiByte( CP_ACP, 0, name.c_str(), -1, ctf, 1000, NULL, NULL );
		std::cout << "name :" << ctf << std::endl;
		//std::wcout << name << std::endl;

		m_MarkupSTL.FindChildElem(_T("desc"));
		std::wstring desc = m_MarkupSTL.GetChildData();
		
		WideCharToMultiByte( CP_ACP, 0, desc.c_str(), -1, ctf, 1000, NULL, NULL );
		std::cout << "decs :" << ctf << std::endl;
		
		//std::wcout << desc << std::endl;
		//wprintf(L"%s\n",desc.c_str());
		
		m_MarkupSTL.OutOfElem();
	}
	
#else //ANSI
#ifdef OLD_MARKUP_VERSION
	std::cout << "old markup " << std::endl;
#else
	std::cout << "ANSI MODLE" << std::endl;
#endif

	const char* src_file = "test.xml";
#ifdef OLD_MARKUP_VERSION
	int MAX_LEN=102400;
	WCHAR wfilebuf [102400] = L"";
	char filebuf[102400] = "";
	FILE*fp = fopen(src_file,"r");
	fseek(fp,0,SEEK_END);
	int nbytes = ftell(fp);
	fseek(fp,0,SEEK_SET);
	fread(filebuf,nbytes,1,fp);
	fclose(fp);
	

	MultiByteToWideChar(CP_UTF8,0,filebuf,-1,wfilebuf,MAX_LEN);
	WideCharToMultiByte(CP_ACP,0,wfilebuf,-1,filebuf,MAX_LEN,NULL,NULL);
	
	m_MarkupSTL.SetDoc(filebuf);
#else	
	
	if(!m_MarkupSTL.Load(src_file)){
		std::cout << "load error" << std::endl;
		return false;
	}
#endif
	m_MarkupSTL.ResetPos();

	while(m_MarkupSTL.FindChildElem("pp_data")){
		std::string id = m_MarkupSTL.GetChildAttrib("id");
		std::cout << "movie id:" << id << std::endl;

		m_MarkupSTL.IntoElem();

		m_MarkupSTL.FindChildElem("url");
		std::string url = m_MarkupSTL.GetChildData();
		std::cout << "url :" << url << std::endl;

		m_MarkupSTL.FindChildElem("img");
		std::string img = m_MarkupSTL.GetChildData();
		std::cout << "img :" << img << std::endl;

		m_MarkupSTL.FindChildElem("name");
		std::string name = m_MarkupSTL.GetChildData();
		std::cout << "name :" << name << std::endl;;

		m_MarkupSTL.FindChildElem("desc");
		std::string desc = m_MarkupSTL.GetChildData();
		std::cout << "desc :" << desc << std::endl;

		m_MarkupSTL.OutOfElem();
	}

#endif
	std::system("PAUSE");
}


你可能感兴趣的:(xml,String,File,null,url,FP)