小编mik*_*ike的帖子

在不进行包装类的情况下,如何封送Jaxb元素列表?

没有实际组成一个编写器并将每个元素附加到字符串上。有没有一种方法可以让JAXB编组器编组对象列表,而我可以在上面给出顶层元素的名称?

我觉得我已经很接近了

//http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html
public <T> String jaxb(Collection<T> o, Class<T> clazz, String plural){
    try {
        ArrayList<T> al = new ArrayList<T>(o.size());
        al.addAll(o);
        JAXBContext jc = JAXBContext.newInstance(ArrayList.class);
        JAXBElement<ArrayList> amenity = new JAXBElement(new QName(plural), ArrayList.class, al);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(amenity, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

但结果仍然是一个空列表

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pluralName/>
Run Code Online (Sandbox Code Playgroud)

有没有一种方法,而不仅仅是手动将xml字符串粘贴在一起?

更新资料

从迈克尔Glavassevich [我已一些帮助能有一点需要注意做到这一点,各个元素是<Item>小号

//http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> String jaxb(Collection<T> elements, Class<T> …
Run Code Online (Sandbox Code Playgroud)

java xml jaxb

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

jaxb ×1

xml ×1