Click here to Skip to main content
15,883,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is mine XML parsing code
C++
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;


}


This is mine XML

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>


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

Can you please guide why is it so?
Posted

1 solution

When searching your code for pIParentNode these lines are found:
C++
MSXML2::IXMLDOMNode *pIParentNode = NULL;
pIParentNode->Release();
pIParentNode = NULL;

So pIParentNode is NULL when you call the Release() member function which will of course fail.
 
Share this answer
 
Comments
Tarun Batra 30-Oct-12 10:34am    
Sir Thanks for the reply can you please help me in one more problem i am not able to detect the tag,can you please tell me what changes should i do?
Jochen Arndt 30-Oct-12 11:01am    
I'm not sure. I have not used XML very often.
But I think you must use the document root to find the elements:
replace docPtr->getElementsByTagName(strFindText) by
docPtr->documentElement->getElementsByTagName(strFindText)
Tarun Batra 30-Oct-12 11:20am    
i doesn't work sir
Jochen Arndt 30-Oct-12 12:08pm    
Please imagine my point of view. All I see is some kind of code. I don't know what you want to do (I assume you want to access a specific tag but you did not say which one). You are sitting in front of your computer with the ability to debug your code and see what happens and at which point the results are not the expected one. So the first thing to do on your side is locating the source line where the problems are (your printf output should already do that). When doing so, you might find the soltion yourself.
Tarun Batra 30-Oct-12 12:40pm    
can you please reply this http://www.codeproject.com/Questions/486368/HowplustoplusaddplusstructureplustoplustheplusSTLp

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900