import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Test") public class Test { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public static void main(String[] args) throws JAXBException { Test re = new Test(); re.setId("id"); re.setName("name"); StringWriter sw = new StringWriter(); JAXBContext jAXBContext = JAXBContext.newInstance(re.getClass()); Marshaller marshaller = jAXBContext.createMarshaller(); marshaller.marshal(re, sw); System.out.println(sw.toString()); } } |
|
來(lái)自: huowufenghuang > 《默認(rèn)》