. 1 public class JaxbUtil { 2 . 3 / ** . 4 * Java object into xml file . 5 * . 6 * @param XMLPath xml file path . 7 * @param Load Java objects .Class . 8 * @return String xml file . 9 * @throws a JAXBException 10 * / . 11 public static String beanToXml (<?> Object obj, Class Load) throws a JAXBException { 12 is The JAXBContext context = to JAXBContext.newInstance (Load); 13 is the Marshaller Marshaller = context.createMarshaller (); 14 marshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, to false ); 15 the StringWriter Writer = new new the StringWriter (); 16 Marshaller.marshal (obj, Writer); . 17 return writer.toString (); 18 is } . 19 20 is / ** 21 is * XML configuration file into an object 22 is * 23 is * @param XMLPath XML file path 24 * @param Load Java objects .Class 25 *@return java对象 26 * @throws JAXBException 27 * @throws IOException 28 */ 29 @SuppressWarnings("unchecked") 30 public static <T> T xmlToBean(String xmlPath, Class<T> load) throws JAXBException, IOException { 31 JAXBContext context = JAXBContext.newInstance(load); 32 Unmarshaller unmarshaller = context.createUnmarshaller(); 33 return (T) unmarshaller.unmarshal(new StringReader(xmlPath)); 34 } 35 36 / ** 37 [ * converted into the JavaBean xml-default encoding. 8 UTF 38 is * 39 * @param obj 40 * @param Writer 41 is * @return 42 is * / 43 is public static String convertToXml (Object obj) { 44 is return convertToXml ( obj, "UTF-. 8" ); 45 } 46 is 47 / ** 48 * is converted into the JavaBean XML 49 * 50 * @param obj 51 is * @param encoding 52 * @return 53 */ 54 public static String convertToXml(Object obj, String encoding) { 55 String result = null; 56 try { 57 JAXBContext context = JAXBContext.newInstance(obj.getClass()); 58 Marshaller marshaller = context.createMarshaller(); 59 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 60 marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); 61 //去掉生成xml的默认报文头 62 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); 63 StringWriter writer = new StringWriter(); 64 writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n "); 65 marshaller.marshal(obj, writer); 66 result = writer.toString(); 67 } catch (Exception e) { 68 e.printStackTrace(); 69 } 70 return result; 71 } 72 73 is / ** 74 * is converted into the JavaBean xml declaration xml removing portion 75 * 76 * @param obj 77 * @param encoding 78 * @return 79 * / 80 public static String convertToXmlIgnoreXmlHead (Object obj, String encoding) { 81 String Result = null ; 82 the try { 83 The JAXBContext context = to JAXBContext.newInstance (obj.getClass ()); 84 the Marshaller Marshaller = context.createMarshaller(); 85 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 86 marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); 87 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); 88 StringWriter writer = new StringWriter(); 89 marshaller.marshal(obj, writer); 90 result = writer.toString(); 91 } catch (Exception e) { 92 e.printStackTrace(); 93 } 94 return result; 95 } 96 97 /** 98 * xml转换成JavaBean 99 * 100 * @param xml 101 * @param c 102 * @return 103 */ 104 @SuppressWarnings("unchecked") 105 public static <T> T converyToJavaBean(String xml, Class<T> c) { 106 T t = null; 107 try { 108 JAXBContext context = JAXBContext.newInstance(c); 109 Unmarshaller unmarshaller = context.createUnmarshaller(); 110 t = (T) unmarshaller.unmarshal(new StringReader(xml)); 111 } catch (Exception e) { 112 e.printStackTrace(); 113 } 114 return t; 115 } 116 117 private static OutputFormat createPrettyPrint() { 118 OutputFormat format = new OutputFormat(); 119 //format.setIndentSize(2); 120 format.setNewLineAfterDeclaration ( to false ); 121 format.setNewlines ( to true ); 122 format.setTrimText ( to false ); 123 format.setPadText ( to false ); 124 return the format; 125 } 126 127 / ** 128 * 129 * @Title: formatXml 130 * @author : humingbo 131 * @date: 2019 Nian 7 18 morning 11:35:08 132 * @Description: xml formatting method 133 *@param str 134 * @return 135 * @throws Exception 136 */ 137 public static String formatXml(String str) throws Exception { 138 Document document = null; 139 document = DocumentHelper.parseText(str); 140 // 格式化输出格式 141 OutputFormat format = createPrettyPrint(); 142 format.setEncoding("UTF-8"); 143 StringWriter writer = newThe StringWriter (); 144 // formatted output stream 145 the XMLWriter XmlWriter = new new the XMLWriter (Writer, the format); 146 // The document is written to the output stream 147 xmlWriter.write (document); 148 xmlWriter.close (); 149 return writer.toString (); 150 } 151 }