Javabean 實體類轉換為xml
阿新 • • 發佈:2019-01-31
public static String getBeanXml(Object object){ String xml = null; try { JAXBContext context = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); StringWriter writer = new StringWriter(); marshaller.marshal(object, writer); xml = writer.toString(); } catch (Exception e) { e.printStackTrace(); } System.out.println(xml); return xml; }