JAXB:如何向内部元素添加属性

时间:2012-10-17 18:37:06

标签: 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>

更新:是否可以使属性可选?如果是,当我解组时,当特权元素没有可选属性正则表达式时,我是否会将正则表达式字段设为false?

UDPATE2:我不想为资源和权限定义单独的类。另外,我不想使用MOXy。 PLS。建议仅针对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;
    }
}

1 个答案:

答案 0 :(得分:3)

因为权限包含一个属性(它实际上是复杂类型),所以必须创建一个类来保存值和属性:

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;
            }
        }
    }
}
相关问题