为什么xml代码会中断? [英] Why the xml code breaks ?

查看:70
本文介绍了为什么xml代码会中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的XML解析代码

This is mine XML parsing code

try 
   {
	 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
	  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(Xmlpath);//XML file to load//uncomment this
      _variant_t varResult((bool)TRUE);//result 
      
	  varResult = docPtr->load(varXml);

      if ((bool)varResult == FALSE)
	  {
		  wLog->WriteErrorLog("failed to load XML file. Check the file name\n");
		  printf("failed to load XML file. Check the file name\n");
  return 1;
	  }
 NodeListPtr = docPtr->getElementsByTagName(strFindText);
 docPtr->documentElement->get_nodeName(&bstrItemText);
 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);
int j = 0;//loop-index variable
					long length;// number of attributes in the collection

					DOMNamedNodeMapPtr = pIDOMNode->attributes;
}
}
}
//Do not forget to release interfaces:
	pIDOMNode->Release();
	pIDOMNode = NULL;
	pIParentNode->Release();
	pIParentNode = NULL;
		
   } 
 catch(...)
   {
	   wLog->WriteErrorLog("Exception occurred while parsing XML\n");
}
 CoUninitialize();
		wLog->WriteDebugLog("Ends GetMachineList\n");
	return 0;


}



这是我的XML



This is mine XML

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"><?xml version="1.0" encoding="UTF-8"?><DataCaptureSettings><ModuleSettings><capture_local_dir>c:\</capture_local_dir><capture_log_dir>c:\log</capture_log_dir><capture_log_level>debug</capture_log_level><capture_request_interval>2</capture_request_interval><capture_connection_interval>2</capture_connection_interval><smtp_server_name>n1</smtp_server_name><smtp_server_port>80</smtp_server_port></ModuleSettings><Machines><Machine><MachineId>0022</MachineId><AccountId>1</AccountId><location_code>LOC_100</location_code><Make>Nipro</Make><Model>Model1</Model><SerialNumber>126649E</SerialNumber><IpAddress>10.10.10.10</IpAddress><Port>80</Port></Machine><Machine><MachineId>3000</MachineId><AccountId>1</AccountId><location_code>LOC_100</location_code><Make>Make3</Make><Model>Model3</Model><SerialNumber>SN3</SerialNumber><IpAddress>30.30.30.30</IpAddress><Port>80</Port></Machine></Machines></DataCaptureSettings></string>




当我运行代码时,显示的输出如下所示




When i run the code my output shown is as below

Root: string


1
Type: element
Node: string
Text: <?xml version="1.0" encoding="UTF-8"?><DataCaptureSettings><ModuleSettings
><capture_local_dir>c:\</capture_local_dir><capture_log_dir>c:\log</capture_log_
dir><capture_log_level>debug</capture_log_level><capture_request_interval>2</cap
ture_request_interval><capture_connection_interval>2</capture_connection_interva
l><smtp_server_name>n1</smtp_server_name><smtp_server_port>80</smtp_server_port>
</ModuleSettings><Machines><Machine><MachineId>0022</MachineId><AccountId>1</Acc
ountId><location_code>LOC_100</location_code><Make>Nipro</Make><Model>Model1</Mo
del><SerialNumber>126649E</SerialNumber><IpAddress>10.10.10.10</IpAddress><Port>
80</Port></Machine><Machine><MachineId>3000</MachineId><AccountId>1</AccountId><
location_code>LOC_100</location_code><Make>Make3</Make><Model>Model3</Model><Ser
ialNumber>SN3</SerialNumber><IpAddress>30.30.30.30</IpAddress><Port>80</Port></M
achine></Machines></DataCaptureSettings>



并且代码在"pIParentNode-> Release();"处中断.



And the code breaks at "pIParentNode->Release();"

Can you please guide why is it so?

推荐答案

在代码中搜索pIParentNode时,会发现以下行:
When searching your code for pIParentNode these lines are found:
MSXML2::IXMLDOMNode *pIParentNode = NULL;
pIParentNode->Release();
pIParentNode = NULL;


因此,当调用Release()成员函数时,pIParentNodeNULL,这当然会失败.


So pIParentNode is NULL when you call the Release() member function which will of course fail.


这篇关于为什么xml代码会中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆