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

📄 xmlnode.cpp

📁 自动投票程序: web 控件操作例程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				m_pNode->removeChild( pNodeList->item[i] );
			}

			RELEASE_PTR(pNodeList);
		}
		catch ( _com_error e )
		{
			TRACE( _T("CXmlNode::Remove failed:%s\n"), e.ErrorMessage());
			ASSERT( FALSE );
			return FALSE;
		}

		return TRUE;
	}


	//-------------------------------------------------------------------------
	// Function Name    :GetChildren
	// Parameter(s)     :void
	// Return           :CXmlNodesPtr
	// Memo             :get all children nodes
	//-------------------------------------------------------------------------
	CXmlNodesPtr CXmlNode::GetChildren()
	{
		ASSERT( m_pNode != NULL );

		CXmlNodesPtr pNodes ( new CXmlNodes() );
		try
		{
			MSXML2::IXMLDOMNodeListPtr pNodeList = NULL;
			pNodeList =	m_pNode->selectNodes(_bstr_t(_T("child::*")));

			pNodes->m_pNodeList = pNodeList;
			RELEASE_PTR(pNodeList);
		}
		catch ( _com_error e )
		{
			TRACE( _T("CXmlNode::GetChildren failed:%s\n"), e.ErrorMessage());
			ASSERT( FALSE );
		}
		return pNodes;
	}




	//-------------------------------------------------------------------------
	// Function Name    :SelectSingleNode
	// Parameter(s)     :LPCTSTR lpszPath		XPATH
	// Return           :CXmlNodePtr
	// Memo             :XPath selectSingleNode
	//-------------------------------------------------------------------------
	CXmlNodePtr CXmlNode::SelectSingleNode(LPCTSTR lpszPath)
	{
		ASSERT( m_pNode != NULL );

		CXmlNodePtr pNode ( new CXmlNode() );

		try
		{
			MSXML2::IXMLDOMNodePtr pItem = NULL;
			pItem = m_pNode->selectSingleNode( _bstr_t(lpszPath) );

			pNode->m_pNode = pItem;
			RELEASE_PTR(pItem);
		}
		catch ( _com_error e )
		{
			TRACE( _T("CXmlNode::SelectSingleNode( %s ) failed:%s\n"), lpszPath, e.ErrorMessage());
			ASSERT( FALSE );
		}

		return pNode;
	}



	//-------------------------------------------------------------------------
	// Function Name    :SelectNodes
	// Parameter(s)     :LPCTSTR lpszPath		XPATH
	// Return           :CXmlNodesPtr
	// Memo             :XPath selectNodes
	//-------------------------------------------------------------------------
	CXmlNodesPtr CXmlNode::SelectNodes(LPCTSTR lpszPath)
	{
		ASSERT( m_pNode != NULL );

		CXmlNodesPtr pNodes ( new CXmlNodes() );
		try
		{
			MSXML2::IXMLDOMNodeListPtr pNodeList = NULL;
			pNodeList =	m_pNode->selectNodes( _bstr_t(lpszPath) );

			pNodes->m_pNodeList = pNodeList;
			RELEASE_PTR(pNodeList);
		}
		catch ( _com_error e )
		{
			TRACE( _T("CXmlNode::SelectNodes failed:%s\n"), e.ErrorMessage());
			ASSERT( FALSE );
		}

		return pNodes;
	}




	//////////////////////////////////////////////////////////////////////////
	// the following methods are getting value

	// get CString value
	CString CXmlNode::GetValue(LPCTSTR lpszValue /* = NULL */ ) const
	{
		CString strValue;
		_GetValue(strValue);

		if( strValue.IsEmpty() &&
			lpszValue != NULL )
		{
			strValue = lpszValue;
			_SetValue(strValue);
		}

		return strValue;
	}

	// get bool value
	bool CXmlNode::GetValue( bool bDefault ) const
	{
		CString strValue;
		_GetValue(strValue);

		if( strValue.CompareNoCase(_T("1")) == 0 )
		{
			return true;
		}
		else if( strValue.CompareNoCase(_T("0")) == 0 )
		{
			return false;
		}
		else
		{
			strValue = bDefault ? "1" : "0";
			_SetValue(strValue);
			return bDefault;
		}	
	}

	// get int value
	int CXmlNode::GetValue( int nDefault ) const
	{
		CString strValue;
		_GetValue(strValue);

		if( strValue.IsEmpty() )
		{
			strValue.Format( _T("%d"), nDefault);
			_SetValue(strValue);
		}

		return _ttoi(strValue);
	}

	// get long value
	long CXmlNode::GetValue( long lDefault ) const
	{
		CString strValue;
		_GetValue(strValue);

		if( strValue.IsEmpty() )
		{
			strValue.Format( _T("%ld"), lDefault);
			_SetValue(strValue);
		}

		return _ttol(strValue);
	}

	// get __int64 value
	__int64 CXmlNode::GetValue( __int64 llDefault ) const
	{
		CString strValue;
		_GetValue(strValue);

		if( strValue.IsEmpty() )
		{
			strValue.Format( _T("%I64d"), llDefault);
			_SetValue(strValue);
		}

		return _ttoi64(strValue);
	}

	// get float value
	float CXmlNode::GetValue( float fDefault ) const
	{
		CString strValue;
		_GetValue(strValue);

		if( strValue.IsEmpty() )
		{
			strValue.Format( _T("%f"), fDefault);
			_SetValue(strValue);
		}

		return static_cast <float> (_tstof(strValue));
	}

	// get double value
	double CXmlNode::GetValue( double dDefault ) const
	{
		CString strValue;
		_GetValue(strValue);

		if( strValue.IsEmpty() )
		{
			strValue.Format( _T("%e"), dDefault);
			_SetValue(strValue);
		}

		return _tstof(strValue);
	}

	// get DWORD value
	DWORD CXmlNode::GetValue( DWORD dwDefault ) const
	{
		CString strValue;
		_GetValue(strValue);

		if( strValue.IsEmpty() )
		{
			strValue.Format( _T("%lu"), dwDefault);
			_SetValue(strValue);
		}

		return _tcstoul(strValue, NULL, 10);
	}


	//////////////////////////////////////////////////////////////////////////
	// the following methods are setting value

	// set LPCTSTR value
	BOOL CXmlNode::SetValue( LPCTSTR lpszValue )
	{
		CString strValue(lpszValue);
		return _SetValue(strValue);
	}

	// set bool value
	BOOL CXmlNode::SetValue( bool bValue )
	{
		CString strValue;
		strValue = bValue ? _T("1") : _T("0");

		return _SetValue(strValue);
	}

	// set int value
	BOOL CXmlNode::SetValue( int nValue )
	{
		CString strValue;
		strValue.Format( _T("%d"), nValue);

		return _SetValue(strValue);
	}

	// set long value
	BOOL CXmlNode::SetValue( long lValue )
	{
		CString strValue;
		strValue.Format( _T("%ld"), lValue);

		return _SetValue(strValue);
	}

	// set __int64 value
	BOOL CXmlNode::SetValue( __int64 llValue )
	{
		CString strValue;
		strValue.Format( _T("%I64d"), llValue);

		return _SetValue(strValue);
	}


	// set float value
	BOOL CXmlNode::SetValue( float fValue )
	{
		CString strValue;
		strValue.Format( _T("%f"), fValue);

		return _SetValue(strValue);
	}

	// set double value
	BOOL CXmlNode::SetValue( double dValue )
	{
		CString strValue;
		strValue.Format( _T("%e"), dValue);

		return _SetValue(strValue);
	}

	// set DWORD value
	BOOL CXmlNode::SetValue( DWORD dwValue )
	{
		CString strValue;
		strValue.Format( _T("%lu"), dwValue);

		return _SetValue(strValue);
	}


	//////////////////////////////////////////////////////////////////////////
	// The following methods are getting attribute

	// get LPCTSTR attribute
	CString CXmlNode::GetAttribute( CString strName, LPCTSTR lpszDefault /* = NULL */) const
	{
		CString strValue;
		_GetAttribute( strName, strValue);

		if( strValue.IsEmpty() &&
			lpszDefault != NULL )
		{
			strValue = lpszDefault;
		}
		return strValue;
	}

	// get bool attribute
	bool CXmlNode::GetAttribute( CString strName, bool bDefault) const
	{
		CString strValue;
		_GetAttribute( strName, strValue);

		if( strValue.CompareNoCase(_T("1")) == 0 )
		{
			return true;
		}
		else if( strValue.CompareNoCase(_T("0")) == 0 )
		{
			return false;
		}
		else
		{
			return bDefault;
		}	
	}

	// get int attribute
	int	CXmlNode::GetAttribute( CString strName, int nDefault) const
	{
		CString strValue;
		_GetAttribute( strName, strValue);

		if( strValue.IsEmpty() )
			strValue.Format( _T("%d"), nDefault);

		return _ttoi(strValue);
	}


	// get long attribute
	long CXmlNode::GetAttribute( CString strName, long lDefault) const
	{
		CString strValue;
		_GetAttribute( strName, strValue);

		if( strValue.IsEmpty() )
			strValue.Format( _T("%ld"), lDefault);

		return _ttol(strValue);
	}

	// get __int64 attribute
	__int64 CXmlNode::GetAttribute( CString strName, __int64 llDefault) const
	{
		CString strValue;
		_GetAttribute( strName, strValue);

		if( strValue.IsEmpty() )
			strValue.Format( _T("%I64d"), llDefault);

		return _ttoi64(strValue);
	}


	// get float attribute
	float CXmlNode::GetAttribute( CString strName, float fDefault) const
	{
		CString strValue;
		_GetAttribute( strName, strValue);

		if( strValue.IsEmpty() )
			strValue.Format( _T("%f"), fDefault);

		return static_cast <float> (_tstof(strValue));
	}

	// get double attribute
	double CXmlNode::GetAttribute( CString strName, double dDefault) const
	{
		CString strValue;
		_GetAttribute( strName, strValue);

		if( strValue.IsEmpty() )
			strValue.Format( _T("%e"), dDefault);

		return _tstof(strValue);
	}

	// get DWORD attribute
	DWORD CXmlNode::GetAttribute( CString strName, DWORD dwDefault) const
	{
		CString strValue;
		_GetAttribute( strName, strValue);

		if( strValue.IsEmpty() )
			strValue.Format( _T("%lu"), dwDefault);

		return _tcstoul(strValue, NULL, 10);
	}


	//////////////////////////////////////////////////////////////////////////
	// The following methods are setting attribute

	// set LPCTSTR attribute
	BOOL CXmlNode::SetAttribute( CString strName
		, LPCTSTR lpszValue
		, CString strPrefix /* = _T("") */
		, CString strNamespaceURI /* = _T("") */
		)
	{
		CString strValue;
		strValue = lpszValue;

		return _SetAttribute( strName, strValue, strPrefix, strNamespaceURI);	
	}

	// set bool attribute
	BOOL CXmlNode::SetAttribute( CString strName
		, bool bValue
		, CString strPrefix /* = _T("") */
		, CString strNamespaceURI /* = _T("") */
		)
	{
		CString strValue;
		strValue = bValue ? _T("1") : _T("0");

		return _SetAttribute( strName, strValue, strPrefix, strNamespaceURI);
	}

	// set int attribute
	BOOL CXmlNode::SetAttribute( CString strName
		, int nValue
		, CString strPrefix /* = _T("") */
		, CString strNamespaceURI /* = _T("") */
		)
	{
		CString strValue;
		strValue.Format( _T("%d"), nValue);

		return _SetAttribute( strName, strValue, strPrefix, strNamespaceURI);
	}

	// set long attribute
	BOOL CXmlNode::SetAttribute( CString strName
		, long lValue
		, CString strPrefix /* = _T("") */
		, CString strNamespaceURI /* = _T("") */
		)
	{
		CString strValue;
		strValue.Format( _T("%ld"), lValue);

		return _SetAttribute( strName, strValue, strPrefix, strNamespaceURI);
	}

	// set __int64 attribute
	BOOL CXmlNode::SetAttribute( CString strName
		, __int64 llValue
		, CString strPrefix /* = _T("") */
		, CString strNamespaceURI /* = _T("") */
		)
	{
		CString strValue;
		strValue.Format( _T("%I64d"), llValue);

		return _SetAttribute( strName, strValue, strPrefix, strNamespaceURI);
	}


	// set float attribute
	BOOL CXmlNode::SetAttribute( CString strName
		, float fValue
		, CString strPrefix /* = _T("") */
		, CString strNamespaceURI /* = _T("") */
		)
	{
		CString strValue;
		strValue.Format( _T("%f"), fValue);

		return _SetAttribute( strName, strValue, strPrefix, strNamespaceURI);
	}

	// set double attribute
	BOOL CXmlNode::SetAttribute( CString strName
		, double dValue
		, CString strPrefix /* = _T("") */
		, CString strNamespaceURI /* = _T("") */
		)
	{
		CString strValue;
		strValue.Format( _T("%e"), dValue);

		return _SetAttribute( strName, strValue, strPrefix, strNamespaceURI);
	}

	// set DWORD attribute
	BOOL CXmlNode::SetAttribute( CString strName
		, DWORD dwValue
		, CString strPrefix /* = _T("") */
		, CString strNamespaceURI /* = _T("") */
		)
	{
		CString strValue;
		strValue.Format( _T("%lu"), dwValue);

		return _SetAttribute( strName, strValue, strPrefix, strNamespaceURI);
	}

	//-------------------------------------------------------------------------
	// Function Name    :GetOuterHTML
	// Parameter(s)     :void
	// Return           :CString
	// Create			:2008-1-14 11:04 王嘉
	// Memo             :Get OuterXml
	//-------------------------------------------------------------------------
	CString CXmlNode::GetOuterXml(void) const
	{
		ASSERT(!IsNull());

		CString strRet = _T("");
		try
		{
			if( !IsNull() )
			{
				HRESULT hr = S_OK;
				BSTR bstr = NULL;
				hr = m_pNode->get_xml(&bstr);

				ASSERT( SUCCEEDED(hr) );	
				strRet = (LPCTSTR)_bstr_t( bstr, true);

				if( bstr != NULL )
				{
					SysFreeString(bstr);
					bstr = NULL;
				}
			}
		}
		catch ( _com_error e )
		{
			TRACE( _T("CXmlNode::GetOuterXml failed:%s\n"), e.ErrorMessage());
			ASSERT( FALSE );
		}
		return strRet;
	}// GetOuterXml


	//-------------------------------------------------------------------------
	// Function Name    :GetInnerXml
	// Parameter(s)     :void
	// Return           :CString
	// Create			:2008-1-14 11:37 王嘉
	// Memo             :get InnerXml
	//-------------------------------------------------------------------------
	CString CXmlNode::GetInnerXml(void) const
	{
		ASSERT(!IsNull());

		CString strRet = GetOuterXml();
		int nFirst = strRet.Find('>');
		int nEnd = strRet.ReverseFind('<');
		if( nFirst > 0 && nEnd > 0 && nEnd > nFirst )
			strRet = strRet.Mid( nFirst + 1, nEnd - nFirst - 1);
		return strRet;
	}// GetOuterXml

}

⌨️ 快捷键说明

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