Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I want to write a C++ code to parse an XML file and extract data from it.

However, I'm an absolute beginner so I just searched Google.
Most people seem to refer an "Xerces" library to do this job, so i went ahead with it.

However, the online documentation of the Xerces library is a little lean on examples and sample programs.
I found an article in Code project on "Writing an XML file using the Xerces library", but none to read from an XML file and extract data from it.

If someone can give me a small sample code to read and extract data from an XML file (using the Xerces library), or if they can direct me to a site/article that gives a small tutorial on the same, I would be really grateful.
Posted
Updated 19-Apr-11 22:12pm
v2
Comments
Dalek Dave 20-Apr-11 4:12am    
Edited for Grammar and Readability.

I am also trying to use XERCES to parse XML files. What I understand from my code debugging is , one has to use the DefaultHandler interface for parsing the XML files. Its following functions must be implemented to have access to individual elements and attributes:

startDocument(); //indates the document start
void startElement() //indiacates the element start, here you can have all attributes to a particular element and iterate to access them
void endDocument();
void endElement() //indiacates that particular element ends

////sample Code
void SVGHandler::startElement(const   XMLCh* const    uri,
									 const   XMLCh* const    localname,
									 const   XMLCh* const    qname,
									 const   Attributes&		attributes)
{
	// The name has to be representable without any escapes
	/*fFormatter  << XMLFormatter::NoEscapes << chOpenAngle ;
	if ( fExpandNS )
	{
		if (XMLString::compareIString(uri,XMLUni::fgZeroLenString) != 0)
			fFormatter  << uri << chColon;
		fFormatter << localname ;
	}
	else
		fFormatter << qname ;*/

	XMLSize_t len = attributes.getLength();
	for (XMLSize_t index = 0; index < len; index++)
	{
		//
		//  Again the name has to be completely representable. But the
		//  attribute can have refs and requires the attribute style
		//  escaping.
		//
		
		const XMLCh * attName , * nodeValue;
		const XMLCh * nodeName;

		nodeName = qname;
		if(wcscmp(nodeName , L"svg")==0)
		{
			attName = attributes.getQName(index);
			if(wcscmp(attName , L"height")==0)
				svgProp.height = _wtol(attributes.getValue(index));
			else if(wcscmp(attName , L"width")==0)
					svgProp.width = _wtol(attributes.getValue(index));
		}
		else if(wcscmp(nodeName , L"g")==0)
		{
			attName = attributes.getQName(index);
			if(wcscmp(attName , L"style")==0)
				groupProp.gStyle.stroke = _wtol(attributes.getValue(index));
		}
		else if(wcscmp(nodeName , L"rect")==0)
		{
			attName = attributes.getQName(index);
			if(wcscmp(attName , L"height")==0)
				rectObj.height = _wtol(attributes.getValue(index));
			else if(wcscmp(attName , L"style")==0)
				rectObj.recStyle.stroke = _wtol(attributes.getValue(index));
			else if(wcscmp(attName , L"width")==0)
				rectObj.width = _wtol(attributes.getValue(index));
			else if(wcscmp(attName , L"x")==0)
				rectObj.xPos = _wtol(attributes.getValue(index));
			else if(wcscmp(attName , L"y")==0)
				rectObj.yPos = _wtol(attributes.getValue(index));
		}
		else if(wcscmp(nodeName , L"text")==0)
		{
			attName = attributes.getQName(index);
			 if(wcscmp(attName , L"x")==0)
				rectObj.xPos = _wtol(attributes.getValue(index));
			else if(wcscmp(attName , L"y")==0)
				rectObj.yPos = _wtol(attributes.getValue(index)); 
		}



	}
	
}
 
Share this answer
 
v2
Comments
Dalek Dave 20-Apr-11 4:13am    
Edited for Code Block.
You probably already have the appropriate example, please read carefully this page[^]. Specifically the DOMPrint sample[^] looks promising.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Mar-11 20:08pm    
My 5. I had to use Xerces when nothing decent from Microsoft was available yet; and Xerces helped to resolve all most tricky problems in a robust way. Cannot imagine any problem with a simple task.
--SA
S.Raaj Nishanth 22-Mar-11 1:38am    
Hi. I have gone through the Xerces C++ Samples given in the documentation. I've also gone through the DOMPrint sample. Both of them give examples of how to use the Xerces executables with options (e.g. DOMCount [options] <XML file | List file> ). I understand that this can be run from a command line utility (or from perl). However, i do not want that. I am writing C++ code to parse the XML. I do not know which Xerces API's i must call in the code in order to extract information from the XML file. Please correct me if my question is wrong.
CPallini 22-Mar-11 3:58am    
You should study the provided source code.
S.Raaj Nishanth 24-Mar-11 5:27am    
Yes you were right. Reading the source code was in deed helpful. Thanks.
CPallini 24-Mar-11 5:27am    
You are welcome.

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