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

你可能感兴趣的:(JAVA)