如何使用@XmlElement注释列表?
我使用以下注释javax.xml.bind.annotation.XmlElement
:
@XmlElement public List<String> getKeywords() {
return keywords;
}
当我整理一些示例内容时,它将生成以下XML:
<keywords>keyword1</keywords><keywords>keyword2</keywords>
我想获取以下XML:
<keywords> <keyword>keyword1</keyword>
<keyword>keyword2</keyword>
</keywords>
我应该使用哪种注释?
我试过了
@XmlElementWrapper@XmlElement(name="keyword")
但是,整个内容消失了,结果是:
<keywords/>
如果仅尝试重命名元素,也会发生相同的情况:
@XmlElement(name="keyword")
我究竟做错了什么?
更新:
这是根据第一个答案更新的类的完整代码,但仍无法正常工作(将其<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;
}
}
我也尝试了以下相同的结果:
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;
}
}
但是,关键字不为空,如下所示,<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;
}
}
编组的代码是(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();
}
}
回答:
您需要利用@XmlElementWrapper
和@XmlElement
。
回答:
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;
}
}
回答:
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);
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><content>
<keywords>
<keyword>foo</keyword>
<keyword>bar</keyword>
</keywords>
</content>
回答:
以下是我博客中几篇文章的链接,这些文章提供了更多信息:
- http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html
- http://blog.bdoughan.com/2012/12/jaxb-representing-null-and-empty.html
以上是 如何使用@XmlElement注释列表? 的全部内容, 来源链接: utcz.com/qa/431909.html