Jaxb 2将不同元素映射到相同属性
我想知道这在 jaxb2 中是否可行。我有一个 xml 可以是 <元素>或<元素2>它永远不会在某个时候出现。它们都是 String 类型,我希望它映射到我的 java 类中的相同属性。 jaxb2 有没有办法配置它?
谢谢 查理
I wonder if this is possible in jaxb2. I have a xml that can be either < element > or < element2 > it will never apear at the some time. They are both of type String and I want it to map to the same property in my java class. Is there a way in jaxb2 to configure that?
Thanks
Charlie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试序列化回 XML 会发生什么?将使用哪个元素名称?
假设您只需要从 XML 反序列化为 Java,那么您可以通过注释 setter 方法而不是字段来完成此操作:
您可能还需要添加
getA()
和getB()< /code> 方法,以便 JAXB 正确识别设置器。
What would happen if you tried to serialize back to XML? Which element name would be used?
Assuming you only need to deserialize from XML to Java, then you can do this by annotating your setter methods instead of your fields:
You might also have to add
getA()
andgetB()
methods in order for JAXB to recognise the setters properly.你可以这样做:
其中:
You can do something like that:
Where: