【问题标题】:jaxb xml attribute binding annotationsjaxb xml 属性绑定注解
【发布时间】:2014-09-23 08:05:09
【问题描述】:

大家好,

我对 jaxb 注释比较陌生。我可以通过使用注释来绑定通用 xml。我想学习如何绑定更复杂的 xml。到目前为止,我已经阅读了其他一些帖子;主要是this post,但我还是有点迷茫。

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

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

aFileName 取决于文件夹中的文件名,而文件内容将是该文件的实际内容

另外,让我有点困惑的另一件事是我将如何分配元素的值。我知道使用数据传输对象时的编组/解组通常是 ObjInst.setter("value")。然后将整个对象传递给编组器/解组器。您将如何使用具有特定属性名称的元素执行此操作?如果你们能提供给我任何帮助,我们将不胜感激。

这是我目前的代码:RequestMsg 类

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

【问题讨论】:

  • 我不确定你到底在问什么。 “你将如何使用具有特定属性名称的元素来执行此操作?” - 做什么? “另外,还有一件事”——那么第一个问题是什么?
  • 那么,什么不起作用?您的注释似乎或多或少没问题。
  • 对不起,我有点罗嗦了。基本上我正在尝试使用注释来整理我上面给出的 xml 示例。我的问题是使用哪个注释可以让我从代码的另一部分获取名称属性
  • 好的,所以您基本上想以编程方式设置将由 JAXB 编组的属性的名称?
  • 我遇到的第一个问题是在 RequestMsg 中设置 file 和 myattr 的值。在代码中,两者都是字符串。因此,如果我尝试在类中使用 setFile 设置器,则会收到错误和建议的快速修复解决方案,以将类型更改为 ElemWithAttr,这也会强制更改 ElemWithAttr 类

标签: java xml spring jaxb annotations


【解决方案1】:

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

【讨论】:

  • 我还没有完全解决它,但它似乎更多地将@XmlAnyAttribute 映射到我的 ElemWithArr 类问题,然后是一个不正确的解决方案。如果并且当我让它工作时,我会发布。感谢您指出正确的方向!
【解决方案2】:

所以我终于得到了我正在研究的示例。我正在提出我的解决方案,以防有一天它可以帮助其他人。

此解决方案不使用@XmlAnyAttribute。相反,我使用@XmlAttribute & @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 类:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 2018-02-14
    • 2011-02-28
    相关资源
    最近更新 更多