CMarkup类使用小结

1. CMarkup类使用非常简单和直观,到 http://www.firstobject.com/dn_markup.htm 下载 Free版的CMarkup类,共两个文件,一个Markup.cpp,一个Markup.h头文件;

2. 我用的是VC6,将cpp实现文件链入工程后,要取消掉“预编译头”功能:【Project】->【Settings】->【C/C++】页面 ->【Precomplied Headers】,选Not Using......

3. 所使用的 MCD_STR类型,CMarkup类会自动在CString、std::string及wstring之间转换;

4. 不支持深入的定位,需要不断的FindElem和IntoElem,对于一个很深的XML文档来说访问很麻烦。就此点,个人认为还不如微软的DOM解析好用,(MSXML SDK6文档)http://download.csdn.net/detail/dijkstar/4858254

下面是一个基于MFC的控制台应用程序(“_MBCS”编译选项):

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Test.h"
#include "Markup.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{

		//
		// 1. 创建一个xml文件
		//
		CMarkup xml1;
		xml1.AddElem( "ORDER" );
		xml1.IntoElem();
		xml1.AddElem( "ITEM" );
		xml1.IntoElem();
		xml1.AddElem( "NAME", "crank casing" );
		xml1.AddElem( "SN", "132487A-J" );
		xml1.AddElem( "QTY", "1" );
		xml1.Save( "./生成的.xml" );
		MCD_STR strXML = xml1.GetDoc();
		printf("1. 创建的XML串: \n%s\n\n", strXML);	//返回所有的XML字符串
/*
<ORDER>
	<ITEM>
		<SN>132487A-J</SN>
		<NAME>crank casing</NAME>
		<QTY>1</QTY>
	</ITEM>
</ORDER>
*/


		//
		// 2. 访问一个xml文件
		// 
		CMarkup xml2;
		xml2.Load( "./生成的.xml" );	//加载方式1
		//xml2.SetDoc( strXML );		//加载方式2
		xml2.FindElem(); // root ORDER element
		xml2.IntoElem(); // inside ORDER
		while ( xml2.FindElem("ITEM") )
		{
			xml2.IntoElem();

			xml2.FindElem( "SN" );
			MCD_STR strSN = xml2.GetData();
			printf("SN = %s\n", strSN);

			xml2.ResetMainPos();	//不管<SN>节点和<QTY>的顺序如何,保证都能找到

			xml2.FindElem( "QTY" );
			printf("QTY = %d\n", atoi( xml2.GetData()));

			xml2.OutOfElem();
		}

		//
		// 3. 创建节点和属性
		//
		CMarkup xml3;
		xml3.AddElem( "ORDER" );
		xml3.IntoElem(); // inside ORDER
		for ( int nItem=0; nItem<1; ++nItem )
		{
			xml3.AddElem( "ITEM" );
			xml3.IntoElem(); // inside ITEM
			xml3.AddElem( "SN", "132487A-J" );
			xml3.AddElem( "NAME", "crank casing" );
			xml3.AddElem( "QTY", "1" );
			xml3.OutOfElem(); // back out to ITEM level
		}
		xml3.AddElem( "SHIPMENT" );
		xml3.IntoElem(); // inside SHIPMENT
		xml3.AddElem( "POC" );
		xml3.SetAttrib( "type", "non-emergency" );
		xml3.IntoElem(); // inside POC
		xml3.AddElem( "NAME", "John Smith" );
		xml3.AddElem( "TEL", "555-1234" );
		printf("\n\n3. 创建的XML串: \n%s\n\n", xml3.GetDoc());	//返回所有的XML字符串
		xml3.Save( "./生成的3.xml" );
/*
<ORDER>
	<ITEM>
		<SN>132487A-J</SN>
		<NAME>crank casing</NAME>
		<QTY>1</QTY>
	</ITEM>
	<SHIPMENT>
		<POC type="non-emergency">
			<NAME>John Smith</NAME>
			<TEL>555-1234</TEL>
		</POC>
	</SHIPMENT>
</ORDER>
*/



	}

	return nRetCode;
}



你可能感兴趣的:(CMarkup类使用小结)