Java 如何使用@XmlElement 注释列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24307106/
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
How to annotate a list using @XmlElement?
提问by user3429010
I have the following annotation using javax.xml.bind.annotation.XmlElement
:
我使用以下注释javax.xml.bind.annotation.XmlElement
:
@XmlElement
public List<String> getKeywords() {
return keywords;
}
Which produces the following XML when I marshall some example content:
当我编组一些示例内容时,它会生成以下 XML:
<keywords>keyword1</keywords>
<keywords>keyword2</keywords>
I would like to get the following XML:
我想获得以下 XML:
<keywords>
<keyword>keyword1</keyword>
<keyword>keyword2</keyword>
</keywords>
What kind of an annotation should I use?
我应该使用什么样的注释?
I've tried
我试过了
@XmlElementWrapper
@XmlElement(name="keyword")
But then the whole content disappears and the result is:
但随后整个内容消失了,结果是:
<keywords/>
The same happens also if I only try to rename the element:
如果我只尝试重命名元素,也会发生同样的情况:
@XmlElement(name="keyword")
What am I doing wrong?
我究竟做错了什么?
UPDATE:
更新:
Here is the updated full code for the class according to the first answers, but it is still not working (the result is an empty list <keywords/>
when marshalled to XML):
这是根据第一个答案更新的类的完整代码,但它仍然无法正常工作(<keywords/>
当编组为 XML 时,结果是一个空列表):
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Content {
private List<String> keywords;
public Content() {}
@XmlElementWrapper(name="keywords")
@XmlElement(name="keyword")
public List<String> getKeywords() {
return keywords;
}
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
}
I also tried the following with the same result:
我还尝试了以下相同的结果:
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Content {
@XmlElementWrapper(name="keywords")
@XmlElement(name="keyword")
private List<String> keywords;
public Content() {}
public List<String> getKeywords() {
return keywords;
}
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
}
However, the keywords are not empty as the following produces <keywords>keyword1</keywords><keywords>keyword2</keywords>
instead of an empty list:
但是,关键字不是空的,因为以下生成<keywords>keyword1</keywords><keywords>keyword2</keywords>
而不是空列表:
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Content {
private List<String> keywords;
public Content() {}
@XmlElement
public List<String> getKeywords() {
return keywords;
}
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
}
The code for marshalling is (JAX-RS):
编组的代码是(JAX-RS):
import java.io.StringWriter;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
@Path("process")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_XML)
public class ContentHandler {
@POST
public Response process(Content content) {
StringWriter stringWriter = new StringWriter();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Content.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(content, stringWriter);
} catch (JAXBException e) {
return Response.serverError().entity(e.getMessage()).build();
}
return Response.ok(stringWriter.toString(), MediaType.APPLICATION_XML).build();
}
}
采纳答案by bdoughan
You need to leverage @XmlElementWrapper
and @XmlElement
.
您需要利用@XmlElementWrapper
和@XmlElement
。
Java Model
Java模型
Content
内容
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Content {
private List<String> keywords;
public Content() {}
@XmlElementWrapper
@XmlElement(name="keyword")
public List<String> getKeywords() {
return keywords;
}
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
}
Demo Code
演示代码
Demo
演示
import java.util.*;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Content.class);
List<String> strings = new ArrayList<String>(2);
strings.add("foo");
strings.add("bar");
Content content = new Content();
content.setKeywords(strings);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(content, System.out);
}
}
Output
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<content>
<keywords>
<keyword>foo</keyword>
<keyword>bar</keyword>
</keywords>
</content>
For More Information
想要查询更多的信息
Below are links to a couple articles from my blog that provide additional information:
以下是我博客中提供更多信息的几篇文章的链接:
回答by Adam Arold
Use this form:
使用这个表格:
@XmlElementWrapper(name="keywords")
@XmlElement(name="keyword")
Please note that if keywords
is emptythen you will get <keywords />
.
请注意,如果keywords
为空,那么您将获得<keywords />
.
Sometimes you will need to add @XmlRootElement
to your class (depends on the context) and the @XmlAccessorType(XmlAccessType.?)
annotation. I usually use @XmlAccessorType(XmlAccessType.FIELD)
and annotate my fieldswith @XmlElement
.
有时您需要添加@XmlRootElement
到您的类(取决于上下文)和@XmlAccessorType(XmlAccessType.?)
注释。我通常使用@XmlAccessorType(XmlAccessType.FIELD)
和标注我的领域有@XmlElement
。
回答by Ashish Shetkar
Above answer by - Blaise Doughan is completely correct
以上答案 - Blaise Doughan 是完全正确的
Another simple way is , even if you don't write the - @XmlElementWrapper
另一个简单的方法是,即使你不写 - @XmlElementWrapper
private List<String> keywords;
@XmlElementWrapper
@XmlElement(name="keyword")
public List<String> getKeywords() {
return keywords;
}
You can use it this way - write the XmlAccessorType on Class level , then XML element name will be same as the class member name - keywords
您可以这样使用它 - 在 Class 级别编写 XmlAccessorType ,然后 XML 元素名称将与类成员名称相同 - 关键字
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Content {
private List<String> keywords;
public Content() {}
public List<String> getKeywords() {
return keywords;
}
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
}