@XmlRootElement在类上,但不在xml中

时间:2019-06-07 13:38:01

标签: java xml jaxb

我有一个用于从中生成xml有效负载的类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "someName", propOrder = {
    "one",
    "two"
})
@XmlRootElement(name = "test")
public class MyClass {
    @XmlElement
    protected String one;
    @XmlElement
    protected String two;
    ...
}

使用如下的对象工厂方法

@XmlElementDecl("Something")
public JAXBElement<MyClass> getMyClassXml(MyClass value) {
  return new JAXBElement<MyClass>(_Something_QNAME, MyClass.class, null, value);

}

我希望肥皂盒包含

<Something>
    <test>
        <one>1</one>
        <two>2</two>
    </test>
</Something>

但我最终还是

<Something>
    <one>1</one>
    <two>2</two>
</Something>

有人遇到类似的东西吗?

4 个答案:

答案 0 :(得分:0)

如果将您的类放入列表中,然后将该列表保存为XML,则它将类似于您要查找的内容。

答案 1 :(得分:0)

您可以尝试以下代码。

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
public class MyClass {

    @XmlElement
    protected String one;
    @XmlElement
    protected String two;

    public String getOne() {
        return one;
    }

    public void setOne(String one) {
        this.one = one;
    }

    public String getTwo() {
        return two;
    }

    public void setTwo(String two) {
        this.two = two;
    }
}



import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

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

  @XmlElement(name = "test")
  private MyClass testClass;

  public MyClass getTestClass() {
    return testClass;
  }

  public void setTestClass(MyClass testClass) {
    this.testClass = testClass;
  }
}

测试类以进行验证。

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

public class Test {
  public static void main(String[] args) throws Exception {
    MyClass testClass = new MyClass();
    testClass.setOne("1");
    testClass.setTwo("2");
    Something something = new Something();
    something.setTestClass(testClass);

    JAXBContext jaxbContext = JAXBContext.newInstance(Something.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(something, System.out);
  }
}

运行测试类后,您将找到以下xml结构。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Something>
    <test>
        <one>1</one>
        <two>2</two>
    </test>
</Something>

因此,在您的情况下,Something应该包含MyClass作为嵌套对象,以使用Jaxb生成所需的XML结构。

答案 2 :(得分:0)

尝试一下,

Something.java

@XmlRootElement
public class Something {

    @XmlElement(name = "test")
    private List<Test> testList;

    public List<Test> getTestList() {
        return testList;
    }

    public void setTestList(List<Test> testList) {
        this.testList = testList;
    }
}

Test.java

@XmlRootElement(name = "test")
public class Test {

    @XmlElement
    private String one;
    @XmlElement
    private String two;

    public String getOne() {
        return one;
    }

    public void setOne(String one) {
        this.one = one;
    }

    public String getTwo() {
        return two;
    }

    public void setTwo(String two) {
        this.two = two;
    }
}

与JAXB一起编组

//Create a test
Test test = new Test();
test.setOne("1");
test.setTwo("2");

//Create a something
Something something = new Something();
//Add test into the testList of something
something.getTestList().add(test);

JAXBContext jaxbContext = JAXBContext.newInstance(Something.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

jaxbMarshaller.marshal(something, new File("something.xml"));

something.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Something>
    <test>
        <one>1</one>
        <two>2</two>
    </test>
</Something>

答案 3 :(得分:0)

我通过修改wsimport生成的xml类,如下解决了该问题:

// @XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "someName"/*, propOrder = {
    "one",
    "two"
} */ )
// @XmlRootElement(name = "test")
@XmlAccessorType(XmlAccessType.NONE)
public class MyClass {
    // @XmlElement
    protected String one;
    // @XmlElement
    protected String two;
    ...

    // my own custom wrapper
    @XmlElement
    protected MyClassWrapper test = new MyClassWrapper(this);
}

我的包装看起来像这样:

@XmlAccessorType(XmlAccessType.NONE)
@XmlType(name = "someNameWrapper", propOrder = {
        "one",
        "two"
})
public class MyClassWrapper {
    public MyClassWrapper() {}
    public MyClassWrapper(MyClass base) {
        this.base = base;
    }
    @XmlElement
    public getOne() { return base.getOne(); }
    @XmlElement
    public getTwo() { return base.getTwo(); }
}