tags:

views:

114

answers:

2

I am parsing a xml file with QXmlDefaultHandler like this:

void Parser::ParseFilename(const char* pFilename)
{
  LOG_DEBUG("Parser::ParseFilename(%s)", pFilename);
  ClearState();

  m_inputFile.setFileName(pFilename);
  QXmlInputSource source( &m_inputFile );
  QXmlSimpleReader reader;

  reader.setContentHandler( this );
  reader.parse( source );
}

I need to know line numbers for error messages. How can I get them?

+1  A: 

Use the exception that is passed to QXmlErrorHandler::error() function. You can set a custom error handler using QXmlReader::setErrorHandler().

Ariya Hidayat
A: 

Answering myself.

I was not completely clear in my question because I didn't write that I need to generate my own custom error messages. For example I need to make complicate validations of certain attributes. Then, if the attribute is invalid, I need to write message like: "Error on line 15454 column 48, attritubute 'number' should be a prime number but is 65536'.

The approach suggested by Ariya Hidayat works only for messages generated by the SAX parser itself (like malformed XML).

For custom messages I need to overload setDocumentLocator ( QXmlLocator * locator ) which tells me about the current locator, and save somewhere the value of the locator like savedLocator = locator; Actual line number is obtained by locator->lineNumber().

danatel