问题描述
我在Intellij-IDEA(org.codehaus.mojo:jaxb2-maven-plugin:1.6)中使用JAXB Maven插件来生成给定对象的xsd文件。
xsd非常大,包含许多类型,其中大部分都是可选的。
I am using JAXB Maven plugin in Intellij-IDEA (org.codehaus.mojo:jaxb2-maven-plugin:1.6) to generate a given xsd file to objects.The xsd is pretty large and contains many types, most of which are optional.
现在当我将XML解组为对象时,缺少的元素表示为null对象。
Right now when I unmarshal an XML to objects, missing elements are represented as null objects.
例如,如果我尝试执行 rootObject.getAccount()。getAccountName()
但是帐户元素从XML中完全丢失,我得到一个 NullPointerException
So for example, if I am trying to do rootObject.getAccount().getAccountName()
but the Account element is missing altogether from the XML, I get a NullPointerException
我想配置JAXB来解组完整的对象树包括那些代表缺失元素的对象。
I would like to configure JAXB to unmarshal the full object tree to include those representing missing elements.
这可能吗?
推荐答案
这实际上是不正确的,因为 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.
如果您想简化代码,请考虑使用。你可能会得到如下代码:
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.
你是对的。但是你特意说解组整个对象树以包含代表缺失元素的那些。我的观点是,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.
目前你可以做的最好的事情。这就是我所做的(检查 null
s)。嗯,不太好,但也很琐碎。
Probably the best thing you can do at the moment. This is what I do (checking for null
s) 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 null
s. 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 path
s 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.
这篇关于如何使用JAXB将缺少的元素解组为空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!