使用JAXB基于条件的Java spring XML绑定 [英] Java spring XML binding based on conditions using JAXB

查看:51
本文介绍了使用JAXB基于条件的Java spring XML绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAXB将XML绑定到对象,是否可以根据诸如以下条件进行绑定

I am binding XML to object using JAXB, is it possible to bind based on conditions such as

api:page元素中绑定href属性,其中属性position等于next.

Bind href attribute in api:page element where attribute position is equal to next.

或将所有记录作为标准列表,然后使用if condition

Or get all records as standard list and then filter out by using if condition

<api:pagination results-count="93" items-per-page="25">
  <api:page position="this" href="https://url1.com" />
  <api:page position="next" href="https://url1.com?pid=2" />
</api:pagination>

推荐答案

无需进行如此简单设置的转换.利用@XmlAnyElement

It is not necessary to make a transformation for such a simple setup.Take advantage of @XmlAnyElement https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/XmlAnyElement.html and just map your collection as a list of Elements.

@XmlElementWrapper(name = "pagination")
@XmlAnyElement
public List<Element> getPages() {
        return pages;
 }

元素为org.w3c.dom.Element

@XmlElementWrapper是可选的,如果需要,您可以映射分页元素.我不确定您是否需要它.

The @XmlElementWrapper is optional, if you want you can map your pagination element. I am not sure you need it though.

然后使用以下方法提取位置:

Then you extract the position with:

page.getAttribute("position")

page.getAttribute("href") 

URL

这篇关于使用JAXB基于条件的Java spring XML绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆