我有以下XML需要读取。在java 1.6上使用JAXB,我该如何为属性regex进行注释?可以将字段设置为布尔类型吗?
<?xml version="1.0" encoding="utf-8"?>
<authStore>
<authList>
<auth>
<resource>res1</resource>
<privilege regex = "true">PRIV_FILE_.+?_READ</privilege>
</auth>
<auth>
<resource>res2</resource>
<privilege>PRIV_FILE_READ</privilege>
</auth>
</authStore>
更新: 是否有可能将属性设置为可选的? 如果是,当我进行反序列化时,当特权元素没有可选属性regex时,我会得到regex字段为false吗?
更新2:我不想为资源和权限定义单独的类。 同样,我不想使用MOXy。 请提供适用于sun / oracle JDK 1.6 JAXB的解决方案。
更新3:我的当前对象模型如下所示
// AuthStore.java
@XmlRootElement
public class AuthStore {
@XmlElementWrapper(name = "authList")
@XmlElement(name = "auth")
private ArrayList<Auth> authList;
public void setAuthList(ArrayList<Auth> authList) {
this.authList = authList;
}
public ArrayList<Auth> getAuthsList() {
return authList;
}
}
// Auth.java
@XmlRootElement(name = "auth")
@XmlType(propOrder = { "resource", "privilege" })
public class Auth
{
private String resource;
private String privilege;
@XmlElement(name = "resource")
public String getResource()
{
return resource;
}
public void setResource(String resource)
{
this.resource = resource;
}
@XmlElement(name = "privilege")
public String getPrivilege()
{
return privilege;
}
public void setPrivilege(String author)
{
this.privilege = author;
}
}
regex
的值是否总是为true
,还是可以改变的? - bdoughan