示例#1
0
void XMLDOMParsingNodes(char *szFileName)
{

   try 
   {
	   //Qualify namespase explicitly to avoid Compiler Error C2872 "ambiguous symbol" during linking.
	   //Now Msxml2.dll use the "MSXML2" namespace
	   //(see http://support.microsoft.com/default.aspx?scid=kb;en-us;316317):
	   MSXML2::IXMLDOMDocumentPtr docPtr;//pointer to DOMDocument object
	   MSXML2::IXMLDOMNodeListPtr NodeListPtr;//indexed access. and iteration through the collection of nodes
	   MSXML2::IXMLDOMNodePtr DOMNodePtr;//pointer to the node

	   MSXML2::IXMLDOMNode *pIDOMNode = NULL;//pointer to element's node
	   MSXML2::IXMLDOMNode *pIParentNode = NULL;//pointer to parent node
	   MSXML2::IXMLDOMNode *pIAttrNode = NULL;//pointer to attribute node
	   MSXML2::IXMLDOMNamedNodeMapPtr DOMNamedNodeMapPtr;//iteration through the collection of attribute nodes
	   MSXML2::IXMLDOMNodeList *childList=NULL;//node list containing the child nodes
	  
	 
	  //Variable with the name of node to find: 
	  BSTR strFindText  = L" ";//" " means to output every node

	  //Variables to store item's name, parent, text and node type:
	  BSTR bstrItemText,bstrItemNode, bstrItemParent,bstrNodeType;

	 //Variables to store attribute's name,type and text:	 
	  BSTR bstrAttrName, bstrAttrType, bstrAttrText;

	  HRESULT hResult;
	  
	  int i = 0;//loop-index variable
	  int n = 0;//lines counter
      

	  //Initialize COM Library:
      CoInitialize(NULL);

	 //Create an instance of the DOMDocument object:
      docPtr.CreateInstance(__uuidof(DOMDocument30));
		
      // Load a document:
	  _variant_t varXml(szFileName);//XML file to load
      _variant_t varResult((bool)TRUE);//result 
      
	  varResult = docPtr->load(varXml);

      if ((bool)varResult == FALSE)
	  {
		 //printf("*** Error:failed to load XML file. ***\n");
		 MessageBox(0,"Error: failed to load XML file. Check the file name.", \
			 "Load XML file",MB_OK |MB_ICONWARNING);
         return;
	  }

	  //Collect all or selected nodes by tag name:
	  NodeListPtr = docPtr->getElementsByTagName(strFindText);
  
	  //Output the number of nodes:
	  //printf("Number of nodes: %d\n", (NodeListPtr->length));

	  //Output root node:
	  docPtr->documentElement->get_nodeName(&bstrItemText);
	  //%ls formatting is for wchar_t* parameter's type (%s for char* type):
	  printf("\nRoot: %ls\n", bstrItemText);	
	  

	  for(i = 0; i < (NodeListPtr->length); i++)
	  {
		
			if (pIDOMNode) pIDOMNode->Release();			
			NodeListPtr->get_item(i, &pIDOMNode);
			
			
			if(pIDOMNode )
			{				
					
				pIDOMNode->get_nodeTypeString(&bstrNodeType);
				
				//We process only elements (nodes of "element" type): 
				BSTR temp = L"element";
			
				if (lstrcmp((LPCTSTR)bstrNodeType, (LPCTSTR)temp)==0) 
				{
					n++;//element node's number
					printf("\n\n%d\n", n);//element node's number
					printf("Type: %ls\n", bstrNodeType);

					pIDOMNode->get_nodeName(&bstrItemNode);
					printf("Node: %ls\n", bstrItemNode);				
								 
					pIDOMNode->get_text(&bstrItemText);
					printf("Text: %ls\n", bstrItemText);
					
					pIDOMNode->get_parentNode(&pIParentNode);
					pIParentNode->get_nodeName(&bstrItemParent);
					printf("Parent: %ls\n",bstrItemParent);
					
			
					pIDOMNode->get_childNodes(&childList);
					printf("Child nodes: %d\n", (childList->length));
					
					//Get the attributes:
					int j = 0;//loop-index variable
					long length;// number of attributes in the collection

					DOMNamedNodeMapPtr = pIDOMNode->attributes;
				
					hResult = DOMNamedNodeMapPtr->get_length(&length);
	
					if(SUCCEEDED(hResult))
					{
					//Loop through the number of attributes:
						for(j = 0; j < length; j++)
						{														
							//get attribute node:							
							DOMNamedNodeMapPtr->get_item(j,&pIAttrNode);

							pIAttrNode->get_nodeTypeString(&bstrAttrType);//type as string
							printf("\nAttribute type: %ls\n", bstrAttrType);
							//pIAttrNode->get_nodeType(&bstrAttrType);//enumerated type
							//printf("Attribute type: %d\n", bstrAttrType);
							pIAttrNode->get_nodeName(&bstrAttrName);
							printf("Attribute name: %ls\n", bstrAttrName);
							pIAttrNode->get_text(&bstrAttrText);
							printf("Attribute value: %ls\n", bstrAttrText);						
						
						}
					}					
				//	::MessageBox(NULL, (LPCSTR) bstrItemText,"parsing", MB_OK);		
				}
			}
		}
	//Do not forget to release interfaces:
	pIDOMNode->Release();
	pIDOMNode = NULL;
	pIParentNode->Release();
	pIParentNode = NULL;
		
   } 

   catch(...)
   {
	  MessageBox(NULL, ("*** Exception occurred ***"), ("Error message"), MB_OK);
   }


    CoUninitialize();

}
示例#2
0
文件: XMLPaser.cpp 项目: F5000/spree
// 이 함수의 리턴 값인 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 );
	}
}
示例#3
0
文件: XMLPaser.cpp 项目: F5000/spree
HRESULT XMLPaser::ND_hrGetTableValue( LPCTSTR strTable, LPCTSTR strField, 
		UINT idxRow, TCHAR* pVal )
{
	HRESULT hr = E_FAIL;
	MSXML2::IXMLDOMNode *pNode = NULL;
	long idxColumn = -1;
	long lTableChilds = 0;
	long lfirstRow = 0;

	try
	{
		pNode = ND_pGetTableNode( strTable );

		if ( pNode )
		{
			MSXML2::IXMLDOMNodeList* pRowNodeList;
			pNode->get_childNodes( &pRowNodeList );

			if ( pRowNodeList )
			{
				pRowNodeList->get_length( &lTableChilds );

				MSXML2::IXMLDOMNode *pRowNode;
				BSTR bstrName;
				LPSTR pszName;

				for ( long ll = 0; ll < lTableChilds; ll++ )
				{
					pRowNodeList->get_item( ll, &pRowNode );
					pRowNode->get_baseName( &bstrName );

					// 처음 Row가 나오는 순간
					UnicodeToAnsi( bstrName, &pszName );
					if ( stricmp( pszName, _T("Row") ) == 0 )
					{
						lfirstRow = ll;
						CoTaskMemFree( pszName );
						::SysFreeString( bstrName );
						break;
					}

					CoTaskMemFree( pszName );
					::SysFreeString( bstrName );
				}

				if ( lfirstRow >= lTableChilds )
				{
					return ( E_FAIL );
				}

				pRowNodeList->get_item( lfirstRow, &pRowNode );

				if ( pRowNode )
				{
					MSXML2::IXMLDOMNodeList* pCellNodeList;
					pRowNode->get_childNodes( &pCellNodeList );

					if ( pCellNodeList )
					{
						long lCell = 0;
						pCellNodeList->get_length( &lCell );
						
						for ( long ll = 0; ll < lCell; ll++ )
						{
							MSXML2::IXMLDOMNode *pCellNode;
							pCellNodeList->get_item( ll, &pCellNode );

							if ( pCellNode )
							{
								MSXML2::IXMLDOMNode *pDataNode;
								pCellNode->get_firstChild( &pDataNode );
								if ( pDataNode )
								{
									LPSTR pszColumnName;
									pDataNode->get_text( &bstrName );
									UnicodeToAnsi( bstrName, &pszColumnName );

									if ( stricmp( pszColumnName, strField ) == 0 )
									{
										idxColumn = ll;
									}

									CoTaskMemFree( pszColumnName );
									::SysFreeString( bstrName );
									pDataNode->Release();
								}
								pCellNode->Release();
							}
						}
						pCellNodeList->Release();
					}
					pRowNode->Release();
				}

				pRowNodeList->get_item( idxRow + lfirstRow + 1, &pRowNode );

				if ( pRowNode )
				{
					MSXML2::IXMLDOMNodeList* pCellNodeList;
					pRowNode->get_childNodes( &pCellNodeList );

					if ( pCellNodeList )
					{
						MSXML2::IXMLDOMNode *pDataNode;
						pCellNodeList->get_item( idxColumn, &pDataNode );

						if ( pDataNode )
						{
							BSTR bstrData;
							LPSTR pszData;
							pDataNode->get_text( &bstrData );
							UnicodeToAnsi( bstrData, &pszData );

							strcpy( pVal, pszData );
							hr = S_OK;

							CoTaskMemFree( pszData );
							::SysFreeString( bstrData );
							pDataNode->Release();
						}
						pCellNodeList->Release();
					}
					pRowNode->Release();
				}
				pRowNodeList->Release();
			}
			pNode->Release();
		}

		return ( hr );
	}
	catch(...)
	{
		return ( E_FAIL );
	}
}
示例#4
0
//通过WebService查询在巡防的终端信息,当终端发来GPS数据时,用来判断该终端是否在巡防,
//如果是还要将该GPS数据发给巡防管理服务
BOOL CDBManage_WebServer::WebServer_LoadDB_PatrolDeviceInfo(BOOL isUpdateOnLineDeviceList)
{
	BOOL bRet = FALSE;
	
	if (!gPGisMainCtrlProc.m_BusinessManage_ConnectToGPSCheckServer.m_PatrolDevListData.IsEmpty())
	{
		gPGisMainCtrlProc.m_BusinessManage_ConnectToGPSCheckServer.m_PatrolDevListData.ClearAll();
	}
	
	if(m_bWebServiceConn)
	{
		MSXML2::IXMLDOMDocumentPtr pDoc;
		HRESULT	hr;
		hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
		if(FAILED(hr))
		{  
			gWriteLogApp.WriteLog( FOR_ALARM,_T("%s [SYS] 无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库\n"),CTime::GetCurrentTime().Format("%H:%M:%S"));
			return FALSE;
		}

		CComBSTR hiResult;
		m_pWebService->HYTPGIS_PatroAreaUser_QueryAllDevice(&hiResult);
		char *pStr = NULL;
		pStr = _com_util::ConvertBSTRToString(hiResult);
		CString strXML;
		strXML.Format("<?xml version=\"1.0\" encoding=\"utf-8\" ?><DataSet xmlns=\"http://tempuri.org/\"> %s</DataSet>",pStr);

		VARIANT_BOOL bRet = pDoc->loadXML((_bstr_t)strXML);
		if(bRet == -1)
		{
			MSXML2::IXMLDOMNodeListPtr	 pListNode;
			pListNode = pDoc->selectNodes("//Table");
			if(pListNode != NULL)
			{
				int i;
				long tLen;
				pListNode->get_length(&tLen);
				CString strTemp;

				for(i = 0 ; i < tLen; i++)
				{
					int j = 0;
					MSXML2::IXMLDOMNode	 *pTempNode;
					pListNode->get_item(i,&pTempNode);

					MSXML2::IXMLDOMNodeList *TempChildList;
					pTempNode->get_childNodes(&TempChildList);

					long listCount = 0;
					TempChildList->get_length(&listCount);

					MSXML2::IXMLDOMNode *tempListItem = NULL;
					_variant_t	tempValue;
					for(j = 0 ; j < listCount; j++)
					{
						TempChildList->get_item(j,&tempListItem);

						tempListItem->get_nodeTypedValue(&tempValue);
						strTemp.Format("%s",(char *)(_bstr_t)tempValue);
						strTemp = strTemp.TrimLeft();
						strTemp = strTemp.TrimRight();
						gPGisMainCtrlProc.m_BusinessManage_ConnectToGPSCheckServer.m_PatrolDevListData.AddItem(strTemp);
						if(isUpdateOnLineDeviceList) //当巡防路线修改时为TRUE,此时要更新在线终端的巡防状态字段
						{
							gPGisMainCtrlProc.m_BusinessManage_ConToPub.Update_OnlineDeviceList_PatrolState(strTemp);	//
						}
						TRACE("添加巡防用户信息 %s \n",strTemp);
					}
				} //end for(i = 0

				bRet = TRUE;
                gWriteLogApp.WriteLog( FOR_ALARM,_T("%s [SYS] 加载巡防终端信息 %d 成功 \n"),CTime::GetCurrentTime().Format("%H:%M:%S"),tLen);
				m_bWebConnOK = TRUE;
			}
		}

		if(pStr)
		{
			delete []pStr;
			pStr = NULL;
		}
	}

	return bRet;
}