Java 向JAXB元素添加属性

Java 向JAXB元素添加属性,java,xml,jaxb,Java,Xml,Jaxb,我正在努力进行一些JAXB解析,需要一些指导 本质上,我正在尝试向我的类变量添加属性,我已经使用@XmlElement声明为元素。到目前为止,任何使用@XmlAttribute的尝试都会在类级别设置该属性 我现在得到的是: <DataClass newAttribute="test"> <myElement>I wish this element had an attribute</myElement> <anotherElement>I

我正在努力进行一些JAXB解析,需要一些指导

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

我现在得到的是:

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

我希望这个元素有一个属性
我希望这个元素也有属性
我想这样做:

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

这样更好
这也是
我见过其他帖子使用@XmlValue为单个元素添加属性,但当您有元素时,这不起作用,并且在多个元素上也不起作用

有人想过如何实现这一点吗

谢谢!
Jason

这将创建该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; }
    }
}

注意:我是负责人,也是JAXB2.X()专家组的成员

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

数据类

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

}
演示

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);
    }
}
输出

<?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>

这样更好


  • 您需要发布相关的Java代码,以便我们了解实际情况。我的foo欠了您的债。非常感谢。我没有想过以这种方式使用它,它是基本的OO,但出于某种原因,我没有看到它。再次感谢您。