0

我们正在使用 Castor 将 Castor 对象解组为 XML 字符串。Castor 不会为空对象生成空标签。解组时是否有可用的 API 将其设置为参数?

有一种方法可以通过使用处理程序来处理它并覆盖 convertUponGet 方法以返回空字符串。但是,有没有更好的办法呢?

任何线索都会有所帮助。

4

1 回答 1

0

From what I have seen there are 3 ways of dealing with this in order of best to worst.

  1. Use a GeneralizedFieldHandler as explained in http://stackoverflow.com/questions/9176479/how-to-tell-castor-to-marshall-a-null-field-to-an-empty-tag. The field handler is reusable for other fields and doesn't change the behavior of your class.

  2. Modify your get method for the given field to check for nulls and return an empty string if it is null. This approach changes the behavior of your class so if you have other parts of your code relying on nulls for this field, which also isn't a good idea, you will run into problems.

  3. Modify Castor yourself to return an empty string when a null is encountered. Usually a really bad idea changing a tool you are using, unless you submit it back to the developers of the project to integrate into their code base for future releases. This doesn't seem likely since this issue was brought up back in 2007 http://old.nabble.com/Forcing-marshalling-of-null-empty-values--to9080721.html#a9096375 if not earlier

于 2012-02-14T16:54:32.673 回答