# Xerces XML file reading problem

This topic is 3184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to learn Xerces to use XML files, I figured the first thing I should learn is to open XML files and extract their information so I went to the Xerces programming guide and tried to recreate a sample that should open an XML file. The program compiles but crashes when run, the Debug error is: Unhandled exception at 0x7c812afb in XML_test.exe: Microsoft C++ exception: xercesc_3_0::SAXParseException at memory location 0x0012f2f8.. This debug output is (obviously) only created when I remove the error handling around the " parser->parse(xmlFile); " command. This is the code I compiled: (copy pasted from this website], and a library include added):
    #include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>

#pragma comment(lib, "xerces-c_3.lib") // I added this myself

#include <iostream>

using namespace std;
using namespace xercesc;

int main (int argc, char* args[]) {

try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Error during initialization! :\n"
<< message << "\n";
XMLString::release(&message);
return 1;
}

XercesDOMParser* parser = new XercesDOMParser();
parser->setValidationScheme(XercesDOMParser::Val_Always);
parser->setDoNamespaces(true);    // optional

ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
parser->setErrorHandler(errHandler);

char* xmlFile = "x1.xml";

try {
parser->parse(xmlFile); //<-- here is the problem
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (const DOMException& toCatch) {
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (...) {
cout << "Unexpected Exception \n" ;
return -1;
}

delete parser;
delete errHandler;
return 0;
}


Am I missing something, do I have to include any more libs/header files? and now that I'm posting anyways, I'd like to ask what you think of Xerces and if you would recommend another XML package? Thanks A_B

##### Share on other sites
I'd recommend tinyXML or rapidXML instead. The former is easy to use, the latter is fast and therefore better than tinyXML if you bother to spend the extra few hours required to learn how to use it properly (beware of the in situ parsing, for example).

##### Share on other sites
Sorry for the double post, My browser was stuck after I first tried to post what I wrote so I had to refresh the page, I didn't realize it actually posted while it was stuck.

I'll look into your suggestions but can you see what is wrong in the code I posted?

##### Share on other sites
SAXParseException means that input file is not valid XML.

You can get the line and character number from the exception.

Since majority of XML documents are not valid XML, SAX must often be beaten into submission by disabling certain checks, or by converting input into conforming format.

There are two forms of XML:
- well formed (tags are closed, character encoding is correct)
- valid (schema and contents are valid)

Often, a non-validating parser is enough, unfortunately, it quickly "corrupts" the documents since validity errors are never resolved. If you feel that you need only basic functionality, search for non-validating parser, or disable validation in xerces.

##### Share on other sites
There was indeed an error in my XML file, Thank you.