我创建了一个类似
的类 @XmlRootElement(name="mylist")
public class JaxbList<T>{
@XmlElementWrapper(name="list")
@XmlElement(name="Item")
protected List<T> list;
public JaxbList(){}
public JaxbList(List<T> list){
this.list=list;
}
public List<T> getList(){
return list;
}
}
这是主要课程:
List<String> list = new ArrayList<String>();
list.add("COlumn 1");
list.add("COlumn 2");
list.add("COlumn 3");
@SuppressWarnings({ "rawtypes", "unchecked" })
JaxbList jaxbList = new JaxbList(list);
PDFHandler handler = new PDFHandler();
try {
ByteArrayOutputStream baos = handler.getXMLSource(jaxbList);
handler.createPDFFile(baos, "");
System.out.println(jaxbList);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是我的编组方法:
public ByteArrayOutputStream getXMLSource(JaxbList jaxbList) throws Exception {
JAXBContext context;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
try {
context = JAXBContext.newInstance(JaxbList.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(jaxbList, outStream);
} catch (JAXBException e) {
e.printStackTrace();
}
return outStream;
}
编组方法创建以下XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mylist>
<list>
<Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">COlumn 1</Item>
<Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">COlumn 2</Item>
<Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">COlumn 3</Item>
</list>
</mylist>
现在我要编制一份清单列表:
protected List<List<String>> list
XML输出应如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mylist>
<list>
<Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">COlumn 1</Item>
<Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">COlumn 2</Item>
<Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">COlumn 3</Item>
</list>
<list>
<Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">COlumn 1</Item>
<Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">COlumn 2</Item>
<Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">COlumn 3</Item>
</list>
</mylist>
提前致谢!
答案 0 :(得分:0)
以下是一个示例:
public class Test{
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "mylist")
static class MyList<T> {
@XmlAnyElement
protected Collection<Collection<T>> list;
public MyList() {
}
public MyList(Collection<Collection<T>> list) {
this.list = list;
}
public Collection<List<T>> getList() {
return list;
}
}
@XmlRootElement(name = "sublist")
@XmlAccessorType(XmlAccessType.FIELD)
static class List<T> {
@XmlAnyElement
protected Collection<T> sublist;
public Collection<T> getSublist() {
return sublist;
}
public void setSublist(Collection<T> sublist) {
this.sublist = sublist;
}
public List() {
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
static class Item {
@XmlValue
private String value;
public Item(){
}
public Item(String value){
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public static void main(String[] args){
try {
context = JAXBContext.newInstance(MyList.class, List.class, Item.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(jaxbList, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}