⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cexml.cpp

📁 ARM2410基于wince4.2平台的xml文档操作。包括基本的读取、保存等。此实例是读取SD卡上的xml文档
💻 CPP
字号:
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// CCEXML Parser																						//
// 18.11.2004																							//
// Steve Green																							//
// (C) 2004 Diagnostic Associates Ltd																	//
//////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "PocketPCXML.h"
#include <objsafe.h>
#include <objbase.h>
#include "CEXML.h"

#include <msxml2.h>

#include <atlbase.h>
#pragma warning( push )
#pragma warning( disable: 4018 4786)
#include <string>
#pragma warning( pop )
using namespace std;


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

//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////////////////////////////////////////

CCEXML::CCEXML()
{
	m_iXMLDoc = NULL;
	::CoInitializeEx( NULL, COINIT_MULTITHREADED );
}

CCEXML::~CCEXML()
{
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Initialise MSXML and parse XML Document																//
//////////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CCEXML::InitialiseAndParse( LPCTSTR szXMLFile )
{
	BOOL bInit = TRUE;

	if( m_iXMLDoc )
		m_iXMLDoc.Detach();

	m_iXMLDoc.CoCreateInstance( __uuidof( DOMDocument ) );
	
	m_iXMLDoc->put_async( VARIANT_FALSE );
	CComQIPtr<IObjectSafety,&IID_IObjectSafety> iSafety( m_iXMLDoc );
	if ( iSafety ) 
	{ 
		DWORD dwSupported, dwEnabled; 
		iSafety->GetInterfaceSafetyOptions( IID_IXMLDOMDocument, &dwSupported, &dwEnabled );
		iSafety->SetInterfaceSafetyOptions( IID_IXMLDOMDocument, dwSupported, 0 );
	}


	VARIANT_BOOL bSuccess=false;
	// Can load it from a url/filename...
	HRESULT hr = m_iXMLDoc->load( CComVariant ( szXMLFile ), &bSuccess );
	if( FAILED( hr ) )
	{
		MessageBox( NULL, L"Cannot load XML file", L"Error Loading XML", MB_ICONSTOP );
		return FALSE;
	}

	if( bSuccess )
	{


		CComPtr<IXMLDOMElement> iRooterElm;
		hr = m_iXMLDoc->get_documentElement( &iRooterElm );
		if( FAILED( hr ) || iRooterElm == NULL )  // Empty xml file
		{
			MessageBox( NULL, L"Empty document!", L"Error Loading XML", MB_ICONSTOP );
			return FALSE;		
		}

		IXMLDOMNode* iNode = NULL;
		
		IXMLDOMNodeList* List = NULL;
		iRooterElm->get_childNodes( &List );
		
		long Amount;
		List->get_length( &Amount );
		for( int i = 0; i < Amount; i++ )
		{
			List->get_item( i, &iNode );
			DisplayChildren( (IXMLDOMElement*)iNode );   // This is where the parsing is done
		
		}
		
	}
	else // Lets give some errors as to why we cannot validate the XML supplied to us
	{
		long line, linePos;
		BSTR reason = NULL;
		IXMLDOMParseError* parseError = NULL;

		m_iXMLDoc->get_parseError( &parseError );
		parseError->get_errorCode( &hr );
		parseError->get_line( &line );
		parseError->get_linepos( &linePos );
		parseError->get_reason( &reason );
		
		CString strMsg;
		strMsg.Format( L"Error 0x%.8X on line %d, position %d\r\nReason: %s", hr, line, linePos, CString( reason ) );
		MessageBox( NULL, strMsg, L"Error Loading XML", MB_ICONWARNING );
		SysFreeString( reason );
		return FALSE;

	}
		
	return bInit;
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Set attribute for given node
// SDG.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CCEXML::DASetAttribute( BSTR bstrName, BSTR bstrValue, IXMLDOMNode* pNode, IXMLDOMDocument* pDocument )
{
	IXMLDOMNamedNodeMap* pMap = NULL;
	IXMLDOMAttribute* pAttribute = NULL;
	HRESULT hRes = 0;
	CComVariant attribValue( bstrValue );

	pDocument->createAttribute( bstrName, &pAttribute );
	pAttribute->put_nodeValue( attribValue );
	pNode->get_attributes( &pMap ); 
	pMap->setNamedItem( pAttribute, &pNode );


	return hRes;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Add a child node
// We return the new child node in pOut, hence the pointer to pointer
// SDG.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CCEXML::DAAddChild( BSTR name, int nType, IXMLDOMNode** pOut, IXMLDOMNode* pNode, IXMLDOMDocument* pDocument )
{
	
	HRESULT hRes;
	IXMLDOMNode* pNoddy = NULL;
	CComVariant vType( nType );
	pDocument->createNode( vType, name, L"", &pNoddy );
	hRes = pNode->appendChild( pNoddy, pOut );
	return hRes;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Recurrssive function to parse through XML document node siblings										//
//////////////////////////////////////////////////////////////////////////////////////////////////////////
void CCEXML::DisplayChildren( IXMLDOMElement* pParent )
{
	static IXMLDOMNode* pNextSib = NULL;
	static IXMLDOMNode* pChild = NULL;
	
	if( pParent == NULL )  // Finished child n
	{
		return;
	}
	
	DisplayChild( pParent );
	
	do 
	{
		pNextSib = pChild;
		pParent->get_firstChild( &pChild );
		if( pChild == NULL )
			pNextSib->get_nextSibling( &pChild );
		
		DisplayChildren( (IXMLDOMElement*)pChild );
	} 
	while( pChild != NULL );
	
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Helper function to return attributes associted with an XML Element									//
//////////////////////////////////////////////////////////////////////////////////////////////////////////
CString CCEXML::ParseXML(IXMLDOMElement *node)
{
	HRESULT hr = S_OK;
	BSTR nodeName, nodeValue;
	CComBSTR cName, cValue;
	
	CString strValue;
	
	IXMLDOMNamedNodeMap* namedNodeMap = NULL;
	hr = node->get_attributes( &namedNodeMap );
	if ( SUCCEEDED( hr ) && namedNodeMap != NULL ) {
		long listLength;
		hr = namedNodeMap->get_length( &listLength );
		for(long i = 0; i < listLength; i++) {
			IXMLDOMNode* listItem = NULL;
			hr = namedNodeMap->get_item( i, &listItem );
			
			// node name
			listItem->get_nodeName( &nodeName );
			
			// node value ie. release="v1.0" value is v1.0
			CComVariant nodeVal;
			nodeVal.InternalClear();
			cValue.Empty();
			cName.Empty();
			listItem->get_nodeValue( &nodeVal );
			nodeValue = nodeVal.bstrVal;
			
			cName.AppendBSTR( nodeName );
			cValue.AppendBSTR( nodeValue );
			CString strXML( cName );
			CString strVal( cValue );
			CString s;
			s.Format( L"%s:%s", strXML, strVal );
			strValue += strVal + L",";
		}
	}
	
	return strValue;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Return single attribute associated with Element														//
//////////////////////////////////////////////////////////////////////////////////////////////////////////
CString CCEXML::GetAttributes( CString& strAttribs )
{
	CString strAttrib = L"";
	
	strAttrib = strAttribs.Left(strAttribs.FindOneOf( L"," ) );
	
	strAttribs = strAttribs.Mid(strAttribs.FindOneOf( L"," ) + 1 );
	
	return strAttrib;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Create an Empty DOM Document, this could be used to write to
//////////////////////////////////////////////////////////////////////////////////////////////////////////
CComPtr<IXMLDOMDocument> CCEXML::CreateEmptyDOMDocument()
{

	CComPtr<IXMLDOMDocument> iXMLDoc;

	iXMLDoc.CoCreateInstance( __uuidof( DOMDocument ) );
	
	iXMLDoc->put_async( VARIANT_FALSE );
	CComQIPtr<IObjectSafety,&IID_IObjectSafety> iSafety( m_iXMLDoc );
	if ( iSafety ) 
	{ 
		DWORD dwSupported, dwEnabled; 
		iSafety->GetInterfaceSafetyOptions( IID_IXMLDOMDocument, &dwSupported, &dwEnabled );
		iSafety->SetInterfaceSafetyOptions( IID_IXMLDOMDocument, dwSupported, 0 );
	}

	return iXMLDoc;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -