1. 程式人生 > >Javabean 實體類轉換為xml

Javabean 實體類轉換為xml

 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;				
	 }