简体   繁体   中英

How can we customize XML-Parse Errors of STAX or SAX?

I need to show xml-parse-errors to the frontend (to a user) and therefore i need to reformat the xml-parse-errors of Stax (or Sax) API for this.

I can think of several ways to achieve this, but none of those seems to be supported:

  • Is it possible to retrieve an 'error-code' from Stax or Sax for a given error, and provide a custom error-message for the given 'error-code' ?
  • Is it possible to load a customized Properties-File (or a ResourceBundle) with own error-messages into the framework without using the Ext-Folder of the JRE?

Please note that i specifically do not want the exact error-messages as given from the framework. It is necessary to create a custom error-message for each given error-case.

At the moment it seems to me that the only way to have custom error-messages is to write my own xml-parser, but that cant be the way?

You can write a SAX ErrorHandler which is notified of the errors and can do anything it likes with them, eg changing the message text. But there are no tools to help with this, and no error codes: you'll have to match the actual message text and recognize it.

I believe however that Xerces produces localized error messages for different locales. That suggests there might be some mechanism in the source code for extracting messages from a message file of some kind. I would suggest taking a look at the source code to see if that mechanism can be exploited; perhaps, for example, you could create a custom Locale with your own messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM