jaxb xml属性绑定注释

时间:2014-09-23 08:05:09

标签: java xml spring jaxb annotations

大家好,

我对jaxb注释很新。通过使用注释绑定一般的xml我没问题。我想学习如何绑定更复杂的xml。到目前为止,我已经阅读了其他一些帖子;主要是this post,但我仍然有点失落。

我尝试使用的示例xml就是这个:

<request>
    <model> text </model>
    <file name = aFileName> file contents</file>
</request>

其中aFileName取决于文件夹中的文件名是什么,文件内容是该文件的实际内容

另外,让我有点困惑的另一件事是我如何分配元素的值。我知道在使用数据传输对象时编组/解组也不会是ObjInst.setter(&#34; value&#34;)。然后将整个对象传递给marshaller / unmarshallor。如何使用具有特定属性名称的元素执行此操作?我们非常感谢你们能给我的任何帮助。

这是我到目前为止的代码:RequestMsg class

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "model", "file",})
@XmlRootElement(name = "request")

public class RequestMsg implements Serializable {
    private static final long serialVersionUID = -5003915336631618163L;

    @XmlElement()
    private String model;

    private ElemWithAttr file;

    @XmlPath("file/@myAttr")
    private String myAttr;

// CLASS GETTERS & SETTERS
    public ElemWithAttr getFile(){
        return file;
    }

    public String getModel() {
        return model;
    }

    public void setFile(ElemWithAttr file) {
        this.file = file;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

这是我对ElemWithAttr类的代码:

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;


public class ElemWithAttr {

    @XmlValue
    public String content;

    @XmlAttribute
    public String myAttr;


// CLASS GETTERS & SETTERS
    public String getContent() {
        return content;
    }

    public String getMyAttr() {
        return myAttr;
    }

    public void setContent(String audioString) {
        this.content = audioString;
    }

    public void setMyAttr(String myAttr) {
        this.myAttr = myAttr;
    }
}

2 个答案:

答案 0 :(得分:0)

试试@XmlAnyAttribute。这是唯一允许您根据对象中的某个值更改属性名称的标准注释。映射到Map<QName, String> - 完全限定属性名称为值。

答案 1 :(得分:0)

所以我终于得到了我正在努力工作的例子。我提出我的解决方案,以防有一天它可以帮助其他人。

此解决方案不使用@XmlAnyAttribute。相反,我让它使用@XmlAttribute&amp; @XmlValue。基本上,需要首先设置ElemWithAttr类中的变量。然后,将ElemWithAttr的实例传递给RequestMsg类中的setFile方法。

换句话说,就像这样:

ElemWithAttr xml = new ElemWithAttr();
xml.setValue("MEMEMEME");
xml.setName("HAHAHAHA");

RequestMsg req = new RequestMsg();
req.setModel("TEST");
req.setFile(xml);

输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request>
    <model>test</model>
    <file name="HAHAHAHA">MEMEMEME</file>
</request>

以下是我班级的修改:

RequestMsg类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "model", "file",})
@XmlRootElement(name = "request")

public class RequestMsg implements Serializable {
    private static final long serialVersionUID = -5003915336631618163L;

    @XmlElement()
    protected String model;
    @XmlElement()
    protected ElemWithAttr file;

// CLASS GETTERS & SETTERS
    public ElemWithAttr getFile(){
        return file;
    }

    public String getModel() {
        return model;
    }

    public void setFile(ElemWithAttr file) {
        this.file = file;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

ElemWithAttr Class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "file")
public class ElemWithAttr {


    @XmlAttribute(name = "name")
    protected String name;

    @XmlValue
    protected String Value;

// CLASS GETTERS & SETTERS
    // GETTERS
    public String getName() {
        return name;
    }

    public String getValue() {
        return Value;
    }

    // SETTERS  
    public void setName(String name) {
        this.name = name;
    }

    public void setValue(String value) {
        Value = value;
    }
}