2

我有以下想要阅读的 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>

更新:是否可以使属性可选?如果是,当我解组时,当特权元素没有可选属性 regex 时,我会得到 regex 字段为 false 吗?

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

1 回答 1

5

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

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;
            }
        }
    }
}
于 2012-10-17T19:17:42.823 回答