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

3

我有以下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;
    }
}

你希望你的对象模型是什么样子的? - bdoughan
谢谢。添加了当前模型。我已经让这个东西工作了。除此之外,现在我想添加可选属性“regex”。 - anjanb
regex 的值是否总是为 true,还是可以改变的? - bdoughan
正则表达式的值也可以为false。可以这样想,明天我可以将“regex”重命名为“type”,并具有各种不同的值。例如type="regex"或type="expression"等。我表述清楚了吗? - anjanb
是的,@SamiKorhonen已经发布了正确的解决方案。 - bdoughan
1个回答

5

由于权限包含属性(实际上是复杂类型),因此您必须创建一个类来容纳值和属性:

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

+1 - 我建议注释get方法,或设置@XmlAccessorType(XmlAccesssType.FIELD):http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html - bdoughan
虽然我认为在没有创建setter的情况下注释get方法是行不通的,因为这些setter通常是过时的(例如,在读取配置文件或其他不可变数据对象时),但我同意应该设置@XmlAccessType。 - Sami Korhonen
谢谢。我选择了这个答案,因为Blaise Doughan推荐了它。我会尝试并明天回复您。 - anjanb
原帖作者已经有了一些方法,我不想让他们尝试你的解决方案,结果遇到重复的属性例外。如果只有getter,则你是正确的,那么就不会有冲突。 - bdoughan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接