Castor Marshaller 返回空对象的空标签
我们使用 Castor 将 Castor 对象解组为 XML 字符串。 Castor 不会为空对象生成空标签。是否有 API 可在解组时将其设置为参数?
有一种方法可以通过使用处理程序并重写 ConvertUponGet 方法以返回空字符串来处理它。但是,有更好的办法吗?
任何线索都会有所帮助。
We are using Castor to unmarshall the Castor object to XML string. Castor is not generating Empty tags for null objects. Is there a API available to set it as parameter while unmarshalling?
There is a way to handle it by using the handler and override convertUponGet method to return empty string. But, Is there a better to do it?
Any clues will help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,有 3 种处理此问题的方法(按最好到最差的顺序排列)。
按照 http://stackoverflow 中的说明使用 GeneralizedFieldHandler。 com/questions/9176479/how-to-tell-castor-to-marshall-a-null-field-to-an-empty-tag。字段处理程序可重用于其他字段,并且不会更改类的行为。
修改给定字段的 get 方法以检查是否为 null,如果为 null,则返回一个空字符串。这种方法会改变类的行为,因此如果您的代码的其他部分依赖于该字段的空值(这也不是一个好主意),您将会遇到问题。
自己修改 Castor,使其在遇到 null 时返回空字符串。通常,更改您正在使用的工具是一个非常糟糕的主意,除非您将其提交回项目开发人员以集成到他们的代码库中以供将来版本使用。这似乎不太可能,因为这个问题早在 2007 年就被提出了 http://old.nabble.com/Forcing-marshalling-of-null-empty-values--to9080721.html#a9096375 如果不早的话
From what I have seen there are 3 ways of dealing with this in order of best to worst.
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.
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.
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