0

I have a class extending org.xml.sax.ext.DefaultHandler2 in which I override

public void startElement(String uri, 
    String localName, String qName, Attributes attributes) throws SAXException

which is originally from DefaultHandler2's supertype org.xml.sax.helpers.DefaultHandler.

When I try to parse an XML file encoded in UTF-8 with BOM, it throws an exception

[Fatal Error] :1:1: Content is not allowed in prolog.

as well-documented elsewhere.

Now, I'd like to catch this exception in my code, i.e., my class extending DefaultHandler2, but because the eception is thrown on the method itself, I cannot see a way to do this.

Is there one? Or am I stuck with the original exception?

s.d
  • 4,017
  • 5
  • 35
  • 65
  • Can you please post the stacktrace of the exception you wish to catch – alexbt Jan 13 '17 at 13:42
  • @alexbt: My class is embedded in a CLI OSGi framework, as a bundle, so I cannot properly control error handling, what I do get is `[Fatal Error] :1:1: Content is not allowed in prolog.` – s.d Jan 13 '17 at 13:45

1 Answers1

0

I've solved this by setting my extension of DefaultHandler2 as the error handler to the XML parser's XML reader, as got via parser.getXMLReader() (org.xml.sax.XMLReader has a method setErrorHandler(ErrorHandler errorHandler)...

Overriding public void fatalError(SAXParseException e), I can handle the fatal error.

s.d
  • 4,017
  • 5
  • 35
  • 65