Question

I parse an XML file in C++ using the SAX2 api of Xerces-C. So I do implement the DefaultHandler interface and its functions

void startElement(
    const   XMLCh* const    uri,
    const   XMLCh* const    localname,
    const   XMLCh* const    qname,
    const   xercesc::Attributes&     attrs
);

and

void endElement(
    const   XMLCh* const    uri,
    const   XMLCh* const    localname,
    const   XMLCh* const    qname
);

When the xml file has a syntax error, the thrown SAXParseException gives me the line number where the error occurred and I can print the error line to the user.

In my application it is possible that the syntax is well formed but the contained data doesn't have much sense. In this case I would also like to print the error line to the user. But I didn't find a way to get the current line number, because the xml is syntactically correct and there is no SAXParseException thrown. Is there a way to get the line number of a tag?

Was it helpful?

Solution

Override the setDocumentLocator() method in your class derived from xercesc::DefaultHandler to get hold of the xercesc::Locator object. You can then call its getLineNumber() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top