且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用JAXB将缺少的元素解组为空对象

更新时间:2023-10-15 10:52:58

这实际上是不正确的,因为 null 属性在语义上与这个以某个默认值存根的属性不同。因此,为缺失值设置一些新帐户()帐户属性将不正确。

This would be actually incorrect, as null property is semantically different from this property stubbed with some default value. So setting some new Account() to account property for a missing value would not be correct.

如果您想简化代码,请考虑使用 JAXB2 Fluent API插件。你可能会得到如下代码:

If you want to simplify your code, consider using JAXB2 Fluent API plugin. You'll be probably able get code like:

rootObject.withAccount().getAccountName()

(隐式初始化帐户。)我不是100%确定它是否会以这种方式工作,但绝对值得一试。

(Implicitly initializes account.) I am not 100% sure if it will work that way, but definitely worth giving it a try.

更新

关于你评论的一些注释。

A few notes on your comments.


我查看了你发布的链接,并尝试在网上查找但如果我理解正确的话,它主要用于填充对象中的值而不是读取它们。您发送的链接说:FluentApi插件为bean setter提供Method Chaining。我试图以某种方式避免在尝试访问嵌套类时检查空值。

I looked at the link you've posted and tried to look it up online but if I understand correctly, it is mostly used for populating values in the objects rather than reading them. The link you sent says: The FluentApi plugin provides Method Chaining for bean setters. I was looking for a way to somehow avoid checking for nulls when trying to access nested classes.

你是对的。但是你特意说解组整个对象树以包含代表缺失元素的那些。我的观点是,JAXB填写这些缺失的属性是不正确的,但可以做到这一点,而Fluent API插件是我对一个易于使用的API的关闭案件。但这并没有回答你的问题,因为可能目前没有答案。

You are correct. However you said specifically "unmarshal the full object tree to include those representing missing elements". My point is, it would be incorrect for JAXB to fill in these missing properties, but you can do this and the Fluent API plugin is the closes I would know to an easy-to-use API for this case. But this does not answer your question as, probably, there is no answer at the moment.


那里的评论大多建议要么吮吸它向上并检查沿途的空值,使用反射,或只是捕获错误。我最终选择了第一个性能考虑因素。

The comments there mostly suggest to either suck it up and check for null values along the way, use reflection, or just catch the error. I ended up going with the first option for performance considerations.

目前你可以做的***的事情。这就是我所做的(检查 null s)。嗯,不太好,但也很琐碎。

Probably the best thing you can do at the moment. This is what I do (checking for nulls) all the time. Well, not quite nice, but also quite trivial.

接下来,我有点想知道如何解决这个问题?

Next, I though a bit on how could this be solved at all?

XJC支持插件,可以增加生成的代码。这是一个非常强大的工具,我用这些插件做了很多。但是应该生成什么代码?在Java中它应该是什么样子?

XJC supports plugins which can augment the generated code. This is a very powerful tool, I did a great lot with these plugins. But what code should be generated? How should it look like in Java?

使用默认值填充属性是不正确的。我认为这样做不会带来好结果。

Filling properties with default values is simply not correct. I think gooing this way will not lead to good results.

类似 rootObject.getAccount()。getAccountName()由于 null s,将无效。那么在Java中它会是什么样子?

Something like rootObject.getAccount().getAccountName() won't work due to nulls. So how would it look like in Java?

我想到的***的事情是像 getValueByPath(String propertyPath)$这样的界面c $ c>这样你就可以这样做:

The best thing that came to my mind is a interface like getValueByPath(String propertyPath) so that you could do something like:

rootObject.getValueByPath(`account.accountName`);

getValueByPath(String path)可能是由插件生成。可以生成代码来解析这样的路径而无需任何反射。生成的代码将检查自己的属性并返回目标值,或者在其上调用 getValueByPath(...)或返回 null 如果没有找到/ null

The getValueByPath(String path) could be generated by a plugin. It is possible to generate code which would resolve such paths without any reflection. The generated code would check for own properties and either return the target value, or call getValueByPath(...) on it or return null if not found/null.

但是你不会有自动完成和整个事情会更容易出错。

But then you won't have autocompletion and the whole thing would be much more error prone.