天天看點

用CMarkup類建立xml檔案的方法

Markup.cpp和Markup.h檔案内容在下一篇部落格中

首先添加Markup.cpp和Markup.h到工程

在用使用xml的.cpp檔案中添加頭檔案#include "Markup.h"

設定Markup.cpp的Project Setting 中c/c++在分類預編譯的頭檔案中選擇

“不使用預補償頁眉”

如果是在非MFC工程中使用類CMarkup

編譯如果報錯

fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include 

解決方法:

#ifdef _WINDOWS_

#undef _WINDOWS_

#endif

編譯如果報錯

error LNK2005: [email protected] 已經在 dllmain.obj 中定義

error LNK2005: "void * __cdecl operator new(unsigned int)" ([email protected]@Z) 已經在 LIBCMTD.lib(new.obj) 中定義

error LNK2005: "void __cdecl operator delete(void *)" ([email protected]@Z) 已經在 LIBCMTD.lib(dbgdel.obj) 中定義

解決方法:

則設定工程Porject Setting中的Link,在分類input輸入中的對象/庫子產品最前面添加Nafxcwd.lib Libcmtd.lib

在忽略庫中填寫LIBCMT uafxcwd.lib Libcmtd.lib

這樣就能在非MFC工程中用了

現在要建立的xml檔案如下:

<?xml version="1.0" ?>

<matrixs>

        <matrix id="1" type="0" ip="192.168.16.64" port="5050" user="vorx" password="" serv_port="5700">矩陣</matrix>

        <matrix id="5" type="0" ip="192.168.16.65" port="5050" user="vorx" password="" serv_port="6000">矩陣</matrix>

        <matrix id="6" type="0" ip="192.168.16.66" port="5050" user="vorx" password="" serv_port="6100">矩陣</matrix>

</matrixs>

代碼如下

利用類CMarkup建立一個xml檔案
	//g_sFilefullPath.c_str()為要儲存xml的路徑,擷取路徑的方法在第六篇部落格中
	if(GetFileAttributes(g_sFilefullPath.c_str()) == 0xffffffff) //檔案不存在時
	{
		CMarkup xml;
		xml.SetDoc("<?xml version=\"1.0\" ?>\r\n");   
		xml.AddElem("matrixs");	
		xml.Save(g_sFilefullPath.c_str());
	}

//加載xml檔案,這時如果用到周遊一定要注意指針目前定義的位置
	CMarkup xml;
	BOOL bLoadXml = FALSE;
	bLoadXml = xml.Load(g_sFilefullPath.c_str()); //加載xml檔案
	if (bLoadXml)
	{
		xml.ResetMainPos();//将指針定義到第一個頂級标簽的上一個位置<文本>
		xml.FindElem();//将将指針定義到第一個頂級标簽
		while(xml.FindChildElem("matrix"))
		{
			int matrixID = (int)atoi(xml.GetChildAttrib("id"));
			if (m_InsertMatrixID == matrixID)
			{
				MessageBox("此ID已存在,請重新輸入","提示",MB_OK);
				return;
			}
		}
		//周遊完後,指針在檔案結尾,這時要将指針重新定位到開頭才能進行下面的周遊
		xml.ResetMainPos();
		xml.FindElem();
		while(xml.FindChildElem("matrix"))
		{
			string matrixIP = xml.GetChildAttrib("ip");
			if (strcmp( m_InsertMatrixIP, matrixIP.c_str()) == 0)
			{
				MessageBox("此IP位址已存在,請重新輸入","提示",MB_OK);
				return;
			}
		}
		xml.ResetMainPos();
		xml.FindElem();
		while(xml.FindChildElem("matrix"))
		{
			int matrixSerPort = (int)atoi(xml.GetChildAttrib("serv_port"));
			if (m_InsertServerPort == matrixSerPort)
			{
				MessageBox("此服務代理端口已存在,請重新輸入","提示",MB_OK);
				return;
			}
		}
		//為xml檔案添加子元素
		xml.AddChildElem( "matrix", "矩陣" );//在頂級标簽下面添加子标簽
		//設定屬性
		xml.SetChildAttrib("id",m_InsertMatrixID);
		xml.SetChildAttrib("type",m_InsertType);
		xml.SetChildAttrib("ip",m_InsertMatrixIP);
		xml.SetChildAttrib("port",m_InsertMatrixPort);
		xml.SetChildAttrib("user",m_InsertMatrixUser);
		xml.SetChildAttrib("password",m_InsertMatrixPassword);
		xml.SetChildAttrib("serv_port",m_InsertServerPort);
		xml.Save(g_sFilefullPath.c_str());	
	}
           
//删除子節點
	CMarkup xml;
	xml.Load(g_sFilefullPath.c_str());
	xml.ResetMainPos();
	xml.FindElem();
	while (xml.FindChildElem("matrix"))
	{
		int idAttribute=(int)atoi(xml.GetChildAttrib("id"));
		if (idAttribute==nGetId)
		{
			xml.RemoveChildElem();