Java spring XML binding based on conditions using JAXB

牧云@^-^@ 提交于 2019-12-11 08:06:04

问题


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

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

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>

回答1:


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;
 }

Where Element is org.w3c.dom.Element

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")

and

page.getAttribute("href") 

for the url



来源:https://stackoverflow.com/questions/56777286/java-spring-xml-binding-based-on-conditions-using-jaxb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!