anj*_*anb 2 java attributes annotations jaxb java-6
我有我想阅读的以下 XML。在 java 1.6 上使用 JAXB,如何为属性 regex 注释?我可以将字段设为 boolean 类型吗?
<?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>
Run Code Online (Sandbox Code Playgroud)
更新:是否可以将属性设为可选?如果是,当我解组时,当特权元素没有可选属性 regex 时,我是否会得到 regex 字段为假?
UDPATE2 :我不想为资源和权限定义单独的类。另外,我不想使用 MOXy。请。建议仅适用于 sun/oracle JDK 1.6 JAXB 的解决方案。
UPDATE3:我当前的对象模型是这样的
// 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;
}
}
Run Code Online (Sandbox Code Playgroud)
因为权限包含一个属性(它实际上是复杂类型),所以您必须创建一个类来保存值和属性:
import java.io.InputStream;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlRootElement(name = "authStore")
@XmlAccessorType(XmlAccesssType.FIELD)
public class AuthStore {
public static void main(String []args) throws Exception {
InputStream inputStream = AuthStore.class.getResourceAsStream("test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(AuthStore.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
AuthStore authStore = (AuthStore)unmarshaller.unmarshal(inputStream);
System.out.println(authStore.getAuthList().get(0).getResource());
System.out.println(authStore.getAuthList().get(0).getPrivilege().getRegex());
System.out.println(authStore.getAuthList().get(0).getPrivilege().getValue());
}
@XmlElementWrapper(name = "authList")
@XmlElement(name = "auth")
private List<Auth> authList;
public List<Auth> getAuthList() {
return authList;
}
@XmlAccessorType(XmlAccesssType.FIELD)
public static class Auth {
@XmlElement(name = "resource")
private String resource;
@XmlElement(name = "privilege")
private Privilege privilege;
public String getResource() {
return resource;
}
public Privilege getPrivilege() {
return privilege;
}
@XmlAccessorType(XmlAccesssType.FIELD)
public static class Privilege {
@XmlAttribute(name = "regex")
private Boolean regex;
@XmlValue
private String value;
public Boolean getRegex() {
return regex;
}
public String getValue() {
return value;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6063 次 |
最近记录: |