java 向 JAXB 元素添加属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6769697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:11:42  来源:igfitidea点击:

Adding an Attribute to a JAXB Element

javaxmljaxb

提问by JasonH

I'm struggling with some JAXB parsing and need some guidance.

我正在努力处理一些 JAXB 解析,需要一些指导。

Essentially, I'm trying to add attributes to my class variables that I have already declared as Elements using @XmlElement. So far, any attempt to use @XmlAttribute sets the attribute at the class level.

本质上,我试图将属性添加到我已经使用@XmlElement 声明为元素的类变量中。到目前为止,任何使用 @XmlAttribute 的尝试都会在类级别设置属性。

What I"m currently getting is this:

我目前得到的是:

<DataClass newAttribute="test">
  <myElement>I wish this element had an attribute</myElement>
  <anotherElement>I wish this element had an attribute too</anotherElement>
</DataClass>

I'd like to to do this:

我想这样做:

<DataClass>
  <myElement thisAtt="this is what I'm talking about">This is better</myElement>
  <anotherElement thisAtt="a different attribute here">So is this</anotherElement>
</DataClass>

I've seen other posts add an attribute to a single element using the @XmlValue, but that doesn't work when you have Elements, and won't work on multiple elements.

我已经看到其他帖子使用@XmlValue 向单个元素添加属性,但是当您拥有 Elements 时这不起作用,并且不适用于多个元素。

Does anyone have a thought on how this could be accomplished?

有没有人想过如何做到这一点?

Thanks! Jason

谢谢!杰森

采纳答案by Ryan Stewart

This will create that XML:

这将创建该 XML:

public class JaxbAttributes {
    public static void main(String[] args) throws Exception {
        Marshaller marshaller =
            JAXBContext.newInstance(DataClass.class).createMarshaller();
        StringWriter stringWriter = new StringWriter();
        DataClass dataClass = new DataClass(
               new Foo("this is what I'm talking about", "This is better"),
               new Foo("a different attribute here", "So is this"));
        marshaller.marshal(dataClass, stringWriter);
        System.out.println(stringWriter);
    }

    @XmlRootElement(name = "DataClass")
    @XmlType(propOrder = {"myElement", "anotherElement"})
    static class DataClass {
        private Foo myElement;
        private Foo anotherElement;

        DataClass() {}
        public DataClass(Foo myElement, Foo anotherElement) {
            this.myElement = myElement;
            this.anotherElement = anotherElement;
        }

        public Foo getMyElement() { return myElement; }
        public void setMyElement(Foo myElement) { this.myElement = myElement; }
        public Foo getAnotherElement() { return anotherElement; }
        public void setAnotherElement(Foo anotherElement) { this.anotherElement = anotherElement; }
    }

    static class Foo {
        private String thisAtt;
        private String value;

        Foo() {}
        public Foo(String thisAtt, String value) {
            this.thisAtt = thisAtt;
            this.value = value;
        }

        @XmlAttribute
        public String getThisAtt() { return thisAtt; }
        public void setThisAtt(String thisAtt) { this.thisAtt = thisAtt; }
        @XmlValue
        public String getValue() { return value; }
        public void setValue(String value) { this.value = value; }
    }
}

回答by bdoughan

Note:I'm the EclipseLink JAXB (MOXy)lead, and a member of the JAXB 2.X (JSR-222) expert group.

注意:我是EclipseLink JAXB (MOXy) 的负责人,也是 JAXB 2.X ( JSR-222) 专家组的成员。

Alternatively you could use the @XmlPathextension in MOXy to handle this use case:

或者,您可以使用@XmlPathMOXy 中的扩展来处理此用例:

DataClass

数据类

The @XmlPathannotation can be used with the standard JAXB annotations:

@XmlPath注释可以与标准的JAXB注释一起使用:

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="DataClass")
@XmlType(propOrder={"myElement", "anotherElement"})
public class DataClass {

    private String myElement;
    private String myElementThisAtt;
    private String anotherElement;
    private String anotherElementThisAtt;

    public String getMyElement() {
        return myElement;
    }

    public void setMyElement(String myElement) {
        this.myElement = myElement;
    }

    @XmlPath("myElement/@thisAtt")
    public String getMyElementThisAtt() {
        return myElementThisAtt;
    }

    public void setMyElementThisAtt(String myElementThisAtt) {
        this.myElementThisAtt = myElementThisAtt;
    }

    public String getAnotherElement() {
        return anotherElement;
    }

    public void setAnotherElement(String anotherElement) {
        this.anotherElement = anotherElement;
    }

    @XmlPath("anotherElement/@thisAtt")
    public String getAnotherElementThisAtt() {
        return anotherElementThisAtt;
    }

    public void setAnotherElementThisAtt(String anotherElementThisAtt) {
        this.anotherElementThisAtt = anotherElementThisAtt;
    }

}

Demo

演示

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(DataClass.class);

        DataClass dataClass = new DataClass();
        dataClass.setMyElement("This is better");
        dataClass.setMyElementThisAtt("this is what I'm talking about");
        dataClass.setAnotherElement("So is this");
        dataClass.setAnotherElementThisAtt("a different attribute here");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(dataClass, System.out);
    }
}

Output

输出

<?xml version="1.0" encoding="UTF-8"?>
<DataClass>
   <myElement thisAtt="this is what I'm talking about">This is better</myElement>
   <anotherElement thisAtt="a different attribute here">So is this</anotherElement>
</DataClass>

More Information

更多信息