I have a java program that parses an XML document using xerces API.
My parsing class extends org.apache.xerces.parsers.XMLDocumentParser, overloading the startElement, endElement, characters methods.
Since it's a complicated XML document that is written by hand (mainly some kind of configuration elements), classic validation by xsd or dtd is not enough, and I have to return to the user that the XML document is not valid.
But 1 thing I could not achieve is to add the information in the error messages about the line number (and why not column number too) that is currenlty being parsed and where the error occurs.
I thing this can be possible, because Exceptions (org.apache.xerces.xni.parser.XMLParseException) generated by the parser when the XML document is not XML valid contain these informations.
I've never tried this with xerces, but SAX parsers can store a SAX Locator, from which you can get the line and column numbers as the document is parsed (or after an exception).
It looks like XMLDocumentParser
may be able to do the same thing. Its parent class, AbstractXMLDocumentParser
, has a startDocument method which is passed an XMLLocator
parameter. If you override this method, you can save the XMLLocator
and use its getLineNumber
and getColumnNumber
methods.