Need help with the code using Xerces Parser for C++

Jun 28, 2011 at 2:30pm
Hello There,

I have written the below code to parse the XML file and display some of its contents in the Win32 console.I am able to rum this code and everything works well as far as debugging is concerned.However,I am not able to retrieve the required values from the XML file ,to be precise the code
const XMLCh *xmlTitle = currentElement->getAttribute(_T("TITLE"));//ATTR_TITLE);

is not able to get the value of the Title tag.

Can someone suggest me if I am missing on anything....

XML File:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<CATALOG>
<CD>
<TITLE>Unchain my heart</TITLE>
<ARTIST>Joe Cocker</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>EMI</COMPANY>
<PRICE>8.20</PRICE>
<YEAR>1987</YEAR>
</CD>
</CATALOG>


XMLParser.cpp File :

// XMLParser.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdexcept>
#include <sstream>

#include <xercesc/util/XMLException.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>

#include <sys/stat.h>
//#include <unistd.h>
#include <errno.h>


#include "XMLParser.h"

#define X(str) XStr(str).unicodeForm()
XERCES_CPP_NAMESPACE_USE
using namespace std;



GetConfig::GetConfig()
{
try{
XMLPlatformUtils::Initialize();
}
catch( XMLException& e )
{
char* message = XMLString::transcode( e.getMessage() );
cerr << "XML toolkit initialization error: " << message << endl;
XMLString::release( &message );
}

TAG_CATALOG = XMLString::transcode("CATALOG");
TAG_CD = XMLString::transcode("CD");
ATTR_COMPANY = XMLString::transcode("COMPANY");
ATTR_TITLE = XMLString::transcode("TITLE");

m_ConfigFileParser = new XercesDOMParser;


}
void GetConfig::ReadConfigFile(string &s)throw(runtime_error)
{
//Check if the file is valid

struct stat fileStatus;
int iretStat = stat(s.c_str(), &fileStatus);
if(iretStat == ENOENT)
throw(runtime_error("File name doesn't exist or is empty\n"));
else if(iretStat == ENOTDIR )
throw(runtime_error("File path is not a directory\n"));
else if(iretStat == EACCES )
throw(runtime_error("Permission denied\n"));
else if(iretStat == ENAMETOOLONG)
throw(runtime_error("File cannot be read.\n"));

// Configure DOM parser.
m_ConfigFileParser->setValidationScheme( XercesDOMParser::Val_Never );
m_ConfigFileParser->setDoNamespaces( false );
m_ConfigFileParser->setDoSchema( false );
m_ConfigFileParser->setLoadExternalDTD( false );

try
{
m_ConfigFileParser->parse( s.c_str() );

DOMDocument *xmlDoc = m_ConfigFileParser->getDocument();
DOMElement *xmlElement = xmlDoc->getDocumentElement();
if(!xmlElement) throw runtime_error("No elements in the XML file.");
DOMNodeList *children = xmlElement->getChildNodes();
const XMLSize_t nodeCount = children->getLength();

for( XMLSize_t xx = 0; xx < nodeCount; ++xx )
{
DOMNode* currentNode = children->item(xx);
if( currentNode->getNodeType() && // true is not NULL
currentNode->getNodeType() == DOMNode::ELEMENT_NODE ) // is element
{
try{
DOMElement* currentElement = (DOMElement*) currentNode;//dynamic_cast<DOMElement* >( currentNode );

if( XMLString::equals(currentElement->getTagName(), TAG_CD))
{
//Read Attributes
const XMLCh *xmlTitle = currentElement->getAttribute(_T("TITLE"));//ATTR_TITLE);
m_Title = XMLString::transcode(xmlTitle);

const XMLCh *xmlCompany = currentElement->getAttribute(ATTR_COMPANY);
m_Company = XMLString::transcode(xmlCompany);
break;
}
}
catch(XMLException& e)
{
char* message = xercesc::XMLString::transcode( e.getMessage() );
ostringstream errBuf;
errBuf << "Error parsing file: " << message << flush;
XMLString::release( &message );
}
}
}
}
catch(xercesc::XMLException& e )
{

char* message = xercesc::XMLString::transcode( e.getMessage() );
ostringstream errBuf;
errBuf << "Error parsing file: " << message << flush;
XMLString::release( &message );
}
}

GetConfig::~GetConfig()
{
delete m_ConfigFileParser;
if(m_Company) XMLString::release(&m_Company);
if(m_Title) XMLString::release(&m_Title);

try
{
XMLString::release(&TAG_CATALOG);
XMLString::release(&TAG_CD);
XMLString::release(&ATTR_COMPANY);
XMLString::release(&ATTR_TITLE);
}
catch(...)
{
cout<<"Unknown exceptions in Tag Destructor"<<endl;
}

try
{
XMLPlatformUtils::Terminate();
}
catch(xercesc::XMLException &e)
{
char *message = xercesc::XMLString::transcode(e.getMessage());
cout<<"XML error"<<message<<endl;
XMLString::release(&message);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
string ConfigFile = "CD_catalog.xml";

GetConfig appConfig;
appConfig.ReadConfigFile(ConfigFile);

cout<<"Company name : "<<appConfig.getCompany();
cout<<"Title Name : "<<appConfig.getTitle();
return 0;
}



Thanks in advance.
Jun 28, 2011 at 5:00pm
This may help you. some part of a function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool XXX::GetElementAttr(DOMElement *element, const string& AttrName, string& AttrValue)
{
	WriteDebug(DEBUG_ENTER, L4, "");

	bool breturn = false;
	XMLCh Buffer1[MED_BUFF];
	char Buffer2[MED_BUFF];
	

	XMLString::transcode(AttrName.c_str(), &Buffer1[0], MED_BUFF);
	XMLString::transcode(element->getAttribute(Buffer1), &Buffer2[0], MED_BUFF);
	AttrValue = Buffer2;
	if(AttrValue != "")
		breturn = true;
	
	WriteDebug(DEBUG_EXIT, L4, "");
	return breturn;
}



Edit: And use code tags. without code tags it looks Japanese.
Last edited on Jun 28, 2011 at 5:01pm
Jun 28, 2011 at 5:02pm
And use code tags, without code tags it looks Japanese.
Jun 30, 2011 at 4:15pm
Thanks writetonsharma,but Xerces Parser has a built in API getAttribute(),which I dont need or want to overload. I presume I am missing on one or two steps, which I am unable to figure out,since everything is working ok till the code :
 
if( XMLString::equals(currentElement->getTagName(), TAG_CD))


Topic archived. No new replies allowed.