HRESULT XMLHelper::GetAttribute(MSXML2::IXMLDOMNode *pNode, const TCHAR *pAttr, int *pVal) { MSXML2::IXMLDOMNamedNodeMap *pAttrMap = 0; HRESULT hr = pNode->get_attributes(&pAttrMap); if(hr == S_OK) { BSTR attr = SysAllocString(pAttr); MSXML2::IXMLDOMNode *pAttrNode = 0; hr = pAttrMap->getNamedItem(attr, &pAttrNode); SysFreeString(attr); if(hr == S_OK && pAttrNode) { VARIANTARG varg; VariantInit(&varg); hr = pAttrNode->get_nodeValue(&varg); if(hr == S_OK && varg.vt == VT_BSTR) { *pVal = varg.bstrVal? _wtoi(varg.bstrVal): 0; SysFreeString(varg.bstrVal); } pAttrNode->Release(); } pAttrMap->Release(); } return hr; }
UINT XMLPaser::ND_nGetTableRowCount( LPCTSTR strTable ) { UINT nItemCount = 0; MSXML2::IXMLDOMNode *pNode = NULL; try { pNode = ND_pGetTableNode( strTable ); if ( pNode ) { MSXML2::IXMLDOMNamedNodeMap* pNodeMap; if ( SUCCEEDED( pNode->get_attributes( &pNodeMap ) ) && pNodeMap ) { MSXML2::IXMLDOMNode *pAttributeNode = NULL; for ( long i = 0; i < pNodeMap->Getlength(); i++ ) { pNodeMap->get_item( i, &pAttributeNode ); if ( pAttributeNode ) { BSTR bstrName; _variant_t varRowName; pAttributeNode->get_nodeName( &bstrName ); LPSTR pszName; UnicodeToAnsi( bstrName, &pszName ); if ( strcmp( pszName, _T("ss:ExpandedRowCount") ) == 0 ) { pAttributeNode->get_nodeValue( &varRowName ); if ( varRowName.vt == VT_BSTR ) { LPSTR pszTableName; UnicodeToAnsi( varRowName.bstrVal, &pszTableName ); nItemCount = atoi( pszTableName ); CoTaskMemFree( pszTableName ); } } CoTaskMemFree( pszName ); pAttributeNode->Release(); VariantClear( &varRowName ); ::SysFreeString( bstrName ); } } pNodeMap->Release(); } pNode->Release(); } return ( nItemCount ); } catch(...) { return ( 0 ); } }
// 이 함수의 리턴 값인 IXMLDOMNode 포인터는 쓰고 난 후에 Release를 해야한다. MSXML2::IXMLDOMNode* XMLPaser::ND_pGetTableNode( LPCTSTR strTable ) { MSXML2::IXMLDOMNode *pWorksheetNode = NULL; MSXML2::IXMLDOMNode *pTableNode = NULL; long lFoundWorksheet = -1; try { m_pNodeList->reset(); for ( long i = 0; i < m_lTableCount; i++ ) { m_pNodeList->get_item( i, &pWorksheetNode ); if ( pWorksheetNode ) { MSXML2::IXMLDOMNamedNodeMap* pNodeMap; if ( SUCCEEDED( pWorksheetNode->get_attributes( &pNodeMap ) ) && pNodeMap ) { MSXML2::IXMLDOMNode *pAttributeNode = NULL; pNodeMap->get_item( 0, &pAttributeNode ); if ( pAttributeNode ) { BSTR bstrName; _variant_t varTableName; pAttributeNode->get_nodeName( &bstrName ); LPSTR pszName; UnicodeToAnsi( bstrName, &pszName ); if ( strcmp( pszName, _T("ss:Name") ) == 0 ) { pAttributeNode->get_nodeValue( &varTableName ); if ( varTableName.vt == VT_BSTR ) { LPSTR pszTableName; UnicodeToAnsi( varTableName.bstrVal, &pszTableName ); // Found Worksheet! if ( stricmp( strTable, pszTableName ) == 0 ) { lFoundWorksheet = i; } CoTaskMemFree( pszTableName ); } } CoTaskMemFree( pszName ); pAttributeNode->Release(); VariantClear( &varTableName ); ::SysFreeString( bstrName ); } pNodeMap->Release(); } pWorksheetNode->Release(); } } if ( lFoundWorksheet < 0 ) throw ( 0 ); m_pNodeList->get_item( lFoundWorksheet, &pWorksheetNode ); if ( pWorksheetNode ) { MSXML2::IXMLDOMNodeList* pNodeList; pWorksheetNode->get_childNodes( &pNodeList ); if ( pNodeList ) { for ( long i = 0; i < pNodeList->Getlength(); i++ ) { pNodeList->get_item( i, &pTableNode ); if ( pTableNode ) { BSTR bstrName; pTableNode->get_nodeName( &bstrName ); LPSTR pszName; UnicodeToAnsi( bstrName, &pszName ); if ( strcmp( pszName, _T("Table") ) == 0 ) { CoTaskMemFree( pszName ); ::SysFreeString( bstrName ); pNodeList->Release(); pWorksheetNode->Release(); return ( pTableNode ); } CoTaskMemFree( pszName ); ::SysFreeString( bstrName ); } } pNodeList->Release(); } pWorksheetNode->Release(); } return ( NULL ); } catch(...) { return ( NULL ); } }