📄 xml.cpp
字号:
CString strName;
strName.Format( _tcslen(lpszPrefix) > 0 ? _T("%s:%s") : _T("%s%s"), lpszPrefix, lpszName);
try
{
MSXML2::IXMLDOMNodePtr pChildNode = NULL;
pChildNode = m_pDoc->createNode(_variant_t(_T("element")), _bstr_t(strName), _bstr_t(lpszNamespaceURI) );
ASSERT( pChildNode != NULL );
pChild->m_pNode = pChildNode;
RELEASE_PTR(pChildNode);
}
catch ( _com_error e )
{
TRACE( _T("CXml::CreateNode( %s, %s, %s) failed:%s\n"), lpszName, lpszPrefix, lpszNamespaceURI, e.ErrorMessage());
ASSERT( FALSE );
}
return pChild;
}
//-------------------------------------------------------------------------
// Function Name :AddSelectionNamespace
// Parameter(s) :LPCTSTR lpszPrefix The prefix, should not contain xmlns
// :LPCTSTR lpszURI
// Return :
// Create :2007-8-1 18:06 Jerry.Wang
// Memo :Add the selection namespace for the XPath
//-------------------------------------------------------------------------
void CXml::AddSelectionNamespace( LPCTSTR lpszPrefix, LPCTSTR lpszURI)
{
ASSERT( m_pDoc != NULL );
try
{
m_mpNamespace[lpszPrefix] = lpszURI;
CString strNamespaces = _T("");
std::map< CString, CString>::iterator iter;
for( iter = m_mpNamespace.begin(); iter != m_mpNamespace.end(); iter++)
{
CString strNamespace;
strNamespace.Format( _T("xmlns:%s='%s' "), iter->first, iter->second);
strNamespaces += strNamespace;
}
m_pDoc->setProperty( _T("SelectionNamespaces"), _variant_t((LPCTSTR)strNamespaces));
}
catch ( _com_error e )
{
TRACE( _T("CXml::AddNamespace( %s, %s) failed:%s\n"), lpszPrefix, lpszURI, e.ErrorMessage());
ASSERT( FALSE );
}
}
//-------------------------------------------------------------------------
// Function Name :GetRoot
// Parameter(s) :
// Return :
// Memo :get the root element
//-------------------------------------------------------------------------
CXmlNodePtr CXml::GetRoot(void)
{
ASSERT( m_pDoc != NULL );
CXmlNodePtr pNode( new CXmlNode() );
try
{
MSXML2::IXMLDOMElementPtr pElement = NULL;
HRESULT hr = m_pDoc->get_documentElement(&pElement);
ASSERT( SUCCEEDED(hr) );
ASSERT( pElement != NULL );
pNode->m_pNode = pElement;
RELEASE_PTR(pElement);
}
catch ( _com_error e )
{
TRACE( _T("CXml::GetRoot failed:%s\n"), e.ErrorMessage());
ASSERT( FALSE );
}
return pNode;
}
//-------------------------------------------------------------------------
// Function Name :Save
// Parameter(s) :LPCTSTR lpszFilePath [in, optional]
// Return :BOOL
// Memo :Save the xml file
//-------------------------------------------------------------------------
BOOL CXml::Save(LPCTSTR lpszFilePath /* = NULL */)
{
if( m_pDoc == NULL )
{
ASSERT(FALSE);
return FALSE;
}
HRESULT hr = S_OK;
try
{
if( lpszFilePath == NULL )
{
ASSERT(!m_strFilePath.IsEmpty());
hr = m_pDoc->save( _variant_t((LPCTSTR)m_strFilePath) );
}
else
{
hr = m_pDoc->save( _variant_t( lpszFilePath ) );
if( SUCCEEDED(hr) )
m_strFilePath = lpszFilePath;
}
}
catch ( _com_error e )
{
TRACE( _T("CXml::Save( %s ) failed:%s\n"), lpszFilePath, e.ErrorMessage());
ASSERT( FALSE );
}
return SUCCEEDED(hr);
}
//-------------------------------------------------------------------------
// Function Name :SelectSingleNode
// Parameter(s) :LPCTSTR strPath
// Return :CXmlNodePtr
// Memo :Query node by XPath
//-------------------------------------------------------------------------
CXmlNodePtr CXml::SelectSingleNode(LPCTSTR lpszPath)
{
ASSERT( m_pDoc != NULL );
CXmlNodePtr pNode ( new CXmlNode() );
if( !GetRoot()->IsNull() )
(*pNode) = GetRoot()->SelectSingleNode(lpszPath);
return pNode;
}
//-------------------------------------------------------------------------
// Function Name :SelectNodes
// Parameter(s) :LPCTSTR strPath
// Return :CXmlNodesPtr
// Memo :Query nodes by XPath
//-------------------------------------------------------------------------
CXmlNodesPtr CXml::SelectNodes(LPCTSTR lpszPath)
{
ASSERT( m_pDoc != NULL );
CXmlNodesPtr pNodes( new CXmlNodes() );
if( !GetRoot()->IsNull() )
(*pNodes) = GetRoot()->SelectNodes(lpszPath);
return pNodes;
}
//-------------------------------------------------------------------------
// Function Name :EncodeBase64
// Parameter(s) :LPBYTE *pBuf The binary buffer
// :ULONG ulSize size
// Return :CString the result
// Memo :encoding the binary buffer into Base64 string
//-------------------------------------------------------------------------
CString CXml::Base64Encode(LPBYTE pBuf, ULONG ulSize)
{
ASSERT( m_pDoc != NULL );
ASSERT( pBuf != NULL );
CString strRet = _T("");
try
{
MSXML2::IXMLDOMElementPtr pElement = NULL;
pElement = m_pDoc->createElement( _bstr_t(_T("Base64")) );
ASSERT( pElement != NULL );
HRESULT hr = S_OK;
hr = pElement->put_dataType( _bstr_t(_T("bin.base64")) );
ASSERT( SUCCEEDED(hr) );
SAFEARRAY * pAry = SafeArrayCreateVector( VT_UI1, 0L, ulSize);
::memcpy( pAry->pvData, pBuf, ulSize);
VARIANT var;
VariantInit(&var);
var.parray = pAry;
var.vt = VT_ARRAY | VT_UI1;
pElement->nodeTypedValue = var;
BSTR bstr = NULL;
hr = pElement->get_text( &bstr );
ASSERT( SUCCEEDED(hr) );
strRet = (LPCTSTR)_bstr_t( bstr, true);
if( bstr != NULL )
{
SysFreeString(bstr);
bstr = NULL;
}
RELEASE_PTR(pElement);
}
catch ( _com_error e )
{
TRACE( _T("CXml::Base64Encode failed:%s\n"), e.ErrorMessage());
ASSERT( FALSE );
}
return strRet;
}
//-------------------------------------------------------------------------
// Function Name :Base64Decode
// Parameter(s) :CString strIn The base64 string
// :LPBYTE pBuf The output buffer
// :ULONG * ulSize the site of buffer
// Return :BOOL
// Memo :Decode the base64 string into buffer
// :NOTE: If the pBuf equal to NULL, then the lSize is the buffer length
// :Please see demo
//-------------------------------------------------------------------------
BOOL CXml::Base64Decode(CString strIn IN, LPBYTE pBuf IN OUT, LONG & lSize IN OUT)
{
ASSERT( m_pDoc != NULL );
try
{
MSXML2::IXMLDOMElementPtr pElement = NULL;
pElement = m_pDoc->createElement( _bstr_t(_T("Base64")) );
ASSERT( pElement != NULL );
HRESULT hr = S_OK;
hr = pElement->put_dataType( _bstr_t(_T("bin.base64")) );
ASSERT( SUCCEEDED(hr) );
hr = pElement->put_text( _bstr_t(strIn) );
ASSERT( SUCCEEDED(hr) );
hr = SafeArrayGetUBound( pElement->nodeTypedValue.parray, 1, &lSize);
ASSERT( SUCCEEDED(hr) );
lSize ++;
if( pBuf )
{
memset( pBuf, 0, lSize);
memcpy( pBuf, LPVOID(pElement->nodeTypedValue.parray->pvData), lSize);
}
RELEASE_PTR(pElement);
}
catch ( _com_error e )
{
TRACE( _T("CXml::Base64Decode failed:%s\n"), e.ErrorMessage());
ASSERT( FALSE );
return FALSE;
}
return TRUE;
}
//-------------------------------------------------------------------------
// Function Name :GetVersion
// Parameter(s) :void
// Return :MSXML_VERSION
// Create :2008-1-16 15:05 Jerry.Wang
// Memo :Get the version of current MSXML
//-------------------------------------------------------------------------
MSXML_VERSION CXml::GetVersion(void) const
{
return m_emVersion;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -