如何将 xml 属性添加到 jaxb 注释类 XmlElementWrapper?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3666467/
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-09-06 13:16:11  来源:igfitidea点击:

How can I add xml attributes to jaxb annotated class XmlElementWrapper?

xmlannotationsjaxblinked-list

提问by ABX

I have a class with a XmlElementWrapper annotation like:

我有一个带有 XmlElementWrapper 注释的类,例如:

...

...

  @XmlElementWrapper(name="myList")
    @XmlElements({
    @XmlElement(name="myElement") }
    )
    private List<SomeType> someList = new LinkedList();

... This code produces XML like

...此代码生成 XML 之类的

<myList>
  <myElement> </myElement>
  <myElement> </myElement>
  <myElement> </myElement>
</myList>

so far so good.

到目前为止,一切都很好。

But now I need to add attributes to the list tag to get XML like

但现在我需要向列表标签添加属性以获得 XML 之类的

<myList number="2">
  <myElement> </myElement>
  <myElement> </myElement>
  <myElement> </myElement>
</myList>

Is there a 'smart way to achieve this without creating a new class that contains represents the list?

有没有一种“聪明的方法来实现这一点,而无需创建一个包含代表列表的新类?”

回答by Noby George

I got a better solution for your question.

我为你的问题找到了更好的解决方案。

For making Xml Java object, use the following code:

要制作 Xml Java 对象,请使用以下代码:

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="myList")
public class Root {

    private String number;
    private List<String> someList;

    @XmlAttribute(name="number")
    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @XmlElement(name="myElement")
    public List<String> getSomeList() {
        return someList;
    }

    public void setSomeList(List<String> someList) {
        this.someList = someList;
    } 

    public Root(String numValue,List<String> someListValue) {
        this();
        this.number = numValue;
        this.someList = someListValue;  
    }

    /**
     * 
     */
    public Root() {
        // TODO Auto-generated constructor stub
    }

}

}

To run the above code using JAXB, use the following:

要使用 JAXB 运行上述代码,请使用以下命令:

   import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.*;

public class Demo {

        public static void main(String[] args) throws Exception {
            List<String> arg = new ArrayList<String>();
            arg.add("FOO");
            arg.add("BAR");
            Root root = new Root("123", arg);

            JAXBContext jc = JAXBContext.newInstance(Root.class);
            Marshaller marshaller = jc.createMarshaller();
            marshaller.marshal(root, System.out);
        }
}

This will produce the following XML as the output:

这将生成以下 XML 作为输出:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <myList number="123">
        <myElement>FOO</myElement>
        <myElement>BAR</myElement>
    </myList>

I think this is more helpful you.

我觉得这对你更有帮助。

Thanks..

谢谢..

回答by bdoughan

The MOXyJAXB implementation (I'm the tech lead) has an extension (@XmlPath) to handle this case:

莫西JAXB实现(我是技术主管)具有扩展名(@XmlPath)来处理这种情况:

import java.util.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlPath("myList/@number")
    private int number;

    @XmlElementWrapper(name="myList") 
    @XmlElement(name="myElement") 
    private List<String> someList = new LinkedList<String>();

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public List<String> getSomeList() {
        return someList;
    }

    public void setSomeList(List<String> someList) {
        this.someList = someList;
    } 

}

Will produce the following XML:

将产生以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <myList number="123">
      <myElement>FOO</myElement>
      <myElement>BAR</myElement>
   </myList>
</root>

When this code is run:

运行此代码时:

import javax.xml.bind.*;

public class Demo {

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

        Root root = new Root();
        root.setNumber(123);
        root.getSomeList().add("FOO");
        root.getSomeList().add("BAR");

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

To get this to work using strictly standard JAXB code you will need to use an XML Adapter:

要使用严格的标准 JAXB 代码使其工作,您需要使用 XML 适配器:

Note:

笔记:

To use MOXy JAXB you need to add a file called jaxb.properties in with your model classes with the following entry:

要使用 MOXy JAXB,您需要使用以下条目在模型类中添加一个名为 jaxb.properties 的文件:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

回答by ?eurobur?

If you are not using MOXy or just want to stick to standard JAXB annotations, you can extend upon Noby's answer to add support for a generic wrapper class. Noby's answer only currently supports a list of strings, but say for example you're going to be using the same generic wrapper class for several different classes. In my example, I want to create a generic "PagedList" class that will marshall to something that looks like a list, but also contains information about the page offset and the total number of elements in unpaged list.

如果您不使用 MOXy 或只想坚持使用标准 JAXB 注释,您可以扩展 Noby 的答案以添加对通用包装器类的支持。Noby 的答案目前仅支持字符串列表,但例如,您将要对几个不同的类使用相同的通用包装类。在我的示例中,我想创建一个通用的“PagedList”类,该类将编组为看起来像列表的内容,但还包含有关页面偏移量和未分页列表中元素总数的信息。

The one downside of this solution is that you have to add additional @XmlElement mappings for each type of class that will be wrapped. Overall though, probably a better solution than creating a new class for each pagable elements.

此解决方案的一个缺点是您必须为将被包装的每种类型的类添加额外的 @XmlElement 映射。总的来说,可能比为每个可分页元素创建一个新类更好的解决方案。

@XmlType
public class PagedList<T> {
    @XmlAttribute
    public int offset;

    @XmlAttribute
    public long total;

    @XmlElements({
        @XmlElement(name="order", type=Order.class),
        @XmlElement(name="address", type=Address.class)
        // additional as needed
    })
    public List<T> items;
}

@XmlRootElement(name="customer-profile")
public class CustomerProfile {
    @XmlElement
    public PagedList<Order> orders;
    @XmlElement
    public PagedList<Address> addresses;
}

Marshalling this example would get you:

编组这个例子会让你:

<customer-profile>
    <order offset="1" total="100">
        <order> ... </order>
        <order> ... </order>
        <order> ... </order>
        ...
    </orders>
    <addresses offset="1" total="5">
        <address> ... </address>
        <address> ... </address>
        <address> ... </address>
        <address> ... </address>
        <address> ... </address>
    <addresses>
</customer-profile>

Hope that helps. This is the solution that I settled upon at least.

希望有帮助。这是我至少确定的解决方案。