Java 为什么我的 ArrayList 没有用 JAXB 编组?

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

Why my ArrayList is not marshalled with JAXB?

javaxmljaxb

提问by yegor256

Here is the use case:

这是用例:

@XmlRootElement
public class Book {
  public String title;
  public Book(String t) {
    this.title = t;
  }
}
@XmlRootElement
@XmlSeeAlso({Book.class})
public class Books extends ArrayList<Book> {
  public Books() {
    this.add(new Book("The Sign of the Four"));
  }
}

Then, I'm doing:

然后,我在做:

JAXBContext ctx = JAXBContext.newInstance(Books.class);
Marshaller msh = ctx.createMarshaller();
msh.marshal(new Books(), System.out);

This is what I see:

这是我看到的:

<?xml version="1.0"?>
<books/>

Where are my books? :)

我的书在哪里?:)

采纳答案by Tomas Narros

The elements to be marshalled must be public, or have the XMLElement anotation. The ArrayList class and your class Books do not match any of these rules. You have to define a method to offer the Book values, and anotate it.

要编组的元素必须是公共的,或者具有 XMLElement 注释。ArrayList 类和您的类 Books 不符合任何这些规则。您必须定义一个方法来提供 Book 值,并对其进行注释。

On your code, changing only your Books class adding a "self getter" method:

在您的代码中,仅更改您的 Books 类,添加一个“self getter”方法:

@XmlRootElement
@XmlSeeAlso({Book.class})
public class Books extends ArrayList<Book> {
  public Books() {
    this.add(new Book("The Sign of the Four"));
  }

  @XmlElement(name = "book")
  public List<Book> getBooks() {
    return this;
  }
}

when you run your marshalling code you'll get:

当你运行你的编组代码时,你会得到:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<books><book><title>The Sign of the Four</title></book></books>

(I added a line break for clarity's shake)

(为了清晰起见,我添加了换行符)

回答by musiKk

I don't think you can easily marshall a Listas is. Consider using another class to wrap the list in. The following works:

我认为您无法轻松地按List原样编组 a 。考虑使用另一个类来包装列表。以下工作:

@XmlType
class Book {
    public String title;

    public Book() {
    }

    public Book(String t) {
        this.title = t;
    }
}

@XmlType
class Books extends ArrayList<Book> {
    public Books() {
        this.add(new Book("The Sign of the Four"));
    }
}

@XmlRootElement(name = "books")
class Wrapper {
    public Books book = new Books();
}

Used like the following:

使用方式如下:

JAXBContext ctx = JAXBContext.newInstance(Wrapper.class);
Marshaller msh = ctx.createMarshaller();
msh.marshal(new Wrapper(), System.out);

it produces this result:

它产生这样的结果:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<books><book><title>The Sign of the Four</title></book></books>

回答by Half_Duplex

As @Blaise and @musiKk have pointed out, it would be better to simply have a List of Book in Books, and allow Books to be the true root element. I would not consider the extending of ArrayList an acceptable procedure in my own code.

正如@Blaise 和@musiKk 所指出的那样,最好在 Books 中有一个 Book 列表,并允许 Books 成为真正的根元素。我不会认为在我自己的代码中扩展 ArrayList 是一个可接受的过程。