import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import java.util.*; /** * Created by Administrator on 2014-05-14. */ public class AutoBean{ public static void main(String[] args) throws JAXBException { JAXBContext jaxbContext =JAXBContext.newInstance(SimpleCollection.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING,"utf-8");//編碼格式 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);//是否省略xml頭信息(<?xml version="1.0" encoding="utf-8" standalone="yes"?>) SimpleCollection collection=new SimpleCollection(); List<String> list=new ArrayList<>(); list.add("hello"); list.add("world"); list.add("hello"); Set<String> set=new HashSet<>(list); collection.collection=list; marshaller.marshal(collection, System.out); collection.collection=set; marshaller.marshal(collection, System.out); } } @XmlRootElement class SimpleCollection{ public Collection<String> collection; }