简体   繁体   English

jaxb版本2.0不使用xmlrootelement中的name属性

[英]jaxb version 2.0 not using the name attribute in xmlrootelement

I have updated the jaxb library to version 2.0. 我已将jaxb库更新为2.0版。 I am using the following jars, jaxb-api-2.0.jar and jaxb-imp-2.0.jar. 我使用以下jar,jaxb-api-2.0.jar和jaxb-imp-2.0.jar。 Now the problem is, it's not using the @XmlRootElement(name="something"). 现在的问题是,它没有使用@XmlRootElement(name =“something”)。 But with the help of previous library, the xml used the defined name "something" in the generated xml. 但是在前面的库的帮助下,xml在生成的xml中使用了定义的名称“something”。 Right now it's taking the class name in camel case instead of "something" defined in the name attribute. 现在,它采用驼峰式的类名,而不是name属性中定义的“something”。 Is it a bug of latest jaxb library? 这是最新的jaxb库的错误吗? Please help! 请帮忙!

Below is an example that may help. 以下是一个可能有用的示例。 The @XmlRootElement annotation will control the name of the root element for the XML document. @XmlRootElement注释将控制XML文档的根元素的名称。 If you want to control the name of an inner element you can use the @XmlElement annotation. 如果要控制内部元素的名称,可以使用@XmlElement批注。

SomeObject SomeObject

package forum9272675;

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

@XmlRootElement(name="something")
public class SomeObject {

    private SomethingElse somethingElse;

    @XmlElement(name="something-else")
    public SomethingElse getSomethingElse() {
        return somethingElse;
    }

    public void setSomethingElse(SomethingElse somethingElse) {
        this.somethingElse = somethingElse;
    }

}

SomethingElse SomethingElse

package forum9272675;

public class SomethingElse {

}

Output 产量

<?xml version="1.0" encoding="UTF-8"?>
<something>
    <something-else/>
</something>

In my child class I was using the @XmlRootElement(name="some-thing"). 在我的子类中,我使用的是@XmlRootElement(name =“some-thing”)。 In addition to it, when I use @XmlType(name="some-thing") my problem got resolved! 除此之外,当我使用@XmlType(name =“some-thing”)时,我的问题得到了解决!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM