// CReadxml.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "CReadxml.h"
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <shlwapi.h>
#include "Markup.h"
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define CELL "1111"
#define TRX "2222"
typedef struct _STRUCT_CHECKITEM
{
CString strPy;
map<CString, set<CString>* >* mapItem;
bool operator == ( const _STRUCT_CHECKITEM& val)
{
return val.strPy == strPy;
}
} STRUCT_CHECKITEM;
typedef vector<STRUCT_CHECKITEM> CHECKITEMVECTOR;
typedef map<CString, set<CString>* > MEASITEMMAP;
typedef set<CString> COUNTERSSET;
// 唯一的应用程序对象
CWinApp theApp;
void ConvertCString2Set(const CString& strVal, set<CString>* setOut);
CString ConvertSet2CString(const set<CString>* setOut);
void Read2Xml(const CString& strPath, CHECKITEMVECTOR* vecCheckItem);
void WriteXML(const CString& strPath, CHECKITEMVECTOR* vecCheckItem);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
}
CHECKITEMVECTOR *mapCheckItem = new CHECKITEMVECTOR;
if(NULL != mapCheckItem)
{
Read2Xml("D:\\1.xml", mapCheckItem);
Read2Xml("D:\\2.xml", mapCheckItem);
WriteXML("D:\\out.xml", mapCheckItem);
}
return nRetCode;
}
void Read2Xml(const CString& strPath, CHECKITEMVECTOR* vecCheckItem)
{
CMarkup markUp;
markUp.Load(strPath);
if(markUp.FindElem("root"))
{
markUp.IntoElem();
while(markUp.FindElem("checkitem"))
{
CString strPy = markUp.GetAttrib("name");
//create a object for find;
_STRUCT_CHECKITEM pFind;
pFind.strPy = strPy;
//get the py's struct
_STRUCT_CHECKITEM CheckItemStru;
//determine whether we can find the item
CHECKITEMVECTOR::iterator iterFind = vecCheckItem->begin();
iterFind = find(vecCheckItem->begin(), vecCheckItem->end(), pFind);
//find same py
if(vecCheckItem->end() != iterFind)
{
CheckItemStru = *iterFind;
}
else
{
CheckItemStru.mapItem = new MEASITEMMAP;
CheckItemStru.strPy = strPy;
vecCheckItem->push_back(CheckItemStru);
}
markUp.IntoElem();
while(markUp.FindElem("measset"))
{
CString strMeasset = markUp.GetAttrib("ID");
markUp.IntoElem();
if(markUp.FindElem("measitem"))
{
CString strMeasitem = markUp.GetData();
//the measset map's item
COUNTERSSET* pCountersSet;
//find the measset map's item by measset id
if(CheckItemStru.mapItem->end() != CheckItemStru.mapItem->find(strMeasset))
{
pCountersSet = (*CheckItemStru.mapItem)[strMeasset];
}
else
{
pCountersSet = new COUNTERSSET;
}
ConvertCString2Set(strMeasitem, pCountersSet);
(*CheckItemStru.mapItem)[strMeasset] = pCountersSet;
}
markUp.OutOfElem();
}
markUp.OutOfElem();
}
markUp.OutOfElem();
}
}
void WriteXML(const CString& strPath, CHECKITEMVECTOR* vecCheckItem)
{
ASSERT(NULL != vecCheckItem);
if(NULL == vecCheckItem)
{
return ;
}
if(PathFileExists(strPath))
{
CFile pFile;
if(!pFile.Open(strPath, CFile::modeCreate))
{
return;
}
pFile.Close();
}
CMarkup markUp;
markUp.SetDoc(_T("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"));
markUp.AddElem("root");
markUp.IntoElem();
CHECKITEMVECTOR::iterator iterCheckitem = vecCheckItem->begin();
for(; iterCheckitem != vecCheckItem->end(); ++iterCheckitem)
{
//checkitem
markUp.AddElem("checkitem");
markUp.SetAttrib("name", iterCheckitem->strPy);
markUp.IntoElem();
//measset
MEASITEMMAP::iterator iterMeasset = iterCheckitem->mapItem->begin();
for(; iterMeasset != iterCheckitem->mapItem->end(); ++iterMeasset)
{
CString strVal = ConvertSet2CString(iterMeasset->second);
delete iterMeasset->second;
markUp.AddElem("measset");
markUp.SetAttrib("ID", iterMeasset->first);
markUp.IntoElem();
markUp.AddElem("measitem", strVal);
markUp.OutOfElem();
}
delete iterCheckitem->mapItem;
markUp.OutOfElem();
}
markUp.OutOfElem();
delete vecCheckItem;
markUp.Save(strPath);
}
void ConvertCString2Set(const CString& strVal, set<CString>* setOut)
{
CString str = strVal;
str += ",";
CString strTemp;
size_t nPos = 0;
do
{
nPos = str.Find(",");
strTemp = str.Mid(0,nPos);
str = str.Mid(nPos + 1,str.GetLength());
setOut->insert(strTemp);
}while(str.GetLength() > 0);
};
CString ConvertSet2CString(const set<CString>* setOut)
{
CString strOut;
set<CString>::const_iterator iterStr = setOut->begin();
for(; iterStr != setOut->end(); ++ iterStr)
{
strOut += *iterStr + ",";
}
strOut.Left(strOut.GetLength() - 1);
return CString(strOut);
}
C++XML读写
最新推荐文章于 2024-07-18 15:15:17 发布