简体   繁体   English

我们如何自定义STAX或SAX的XML解析错误?

[英]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. 我需要向前端(向用户)显示xml-parse-errors,因此我需要为此重新格式化Stax(或Sax)API的xml-parse-errors。

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' ? 是否可以从Stax或Sax中检索给定错误的“错误代码”,并为给定“错误代码”提供自定义错误消息?
  • 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? 是否可以在不使用JRE的Ext-Folder的情况下,将具有错误消息的自定义属性文件(或ResourceBundle)加载到框架中?

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? 目前,在我看来,具有自定义错误消息的唯一方法是编写自己的xml解析器,但是那不可能吗?

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. 您可以编写一个通知错误的SAX ErrorHandler,并且可以对它们执行任何喜欢的操作,例如更改消息文本。 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. 但是我相信Xerces会为不同的语言环境生成本地化的错误消息。 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. 例如,也许您可​​以使用自己的消息创建自定义语言环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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