java自带的xml解析工具类

最后都变了- 提交于 2019-11-26 23:05:47
  1 public class JaxbUtil {
  2 
  3     /**
  4      * java对象转换为xml文件
  5      * 
  6      * @param xmlPath xml文件路径
  7      * @param load    java对象.Class
  8      * @return xml文件的String
  9      * @throws JAXBException
 10      */
 11     public static String beanToXml(Object obj, Class<?> load) throws JAXBException {
 12         JAXBContext context = JAXBContext.newInstance(load);
 13         Marshaller marshaller = context.createMarshaller();
 14         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
 15         StringWriter writer = new StringWriter();
 16         marshaller.marshal(obj, writer);
 17         return writer.toString();
 18     }
 19 
 20     /**
 21      * xml文件配置转换为对象
 22      * 
 23      * @param xmlPath xml文件路径
 24      * @param load    java对象.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      * JavaBean转换成xml 默认编码UTF-8
 38      * 
 39      * @param obj
 40      * @param writer
 41      * @return
 42      */
 43     public static String convertToXml(Object obj) {
 44         return convertToXml(obj, "UTF-8");
 45     }
 46 
 47     /**
 48      * JavaBean转换成xml
 49      * 
 50      * @param obj
 51      * @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     /**
 74      * JavaBean转换成xml去除xml声明部分
 75      * 
 76      * @param obj
 77      * @param encoding
 78      * @return
 79      */
 80     public static String convertToXmlIgnoreXmlHead(Object obj, String encoding) {
 81         String result = null;
 82         try {
 83             JAXBContext context = JAXBContext.newInstance(obj.getClass());
 84             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(false);
121         format.setNewlines(true);
122         format.setTrimText(false);
123         format.setPadText(false);
124         return format;
125     }
126 
127     /**
128      * 
129      * @Title: formatXml
130      * @author:humingbo
131      * @date:2019年7月18日上午11:35:08
132      * @Description: 格式化xml方法
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 = new StringWriter();
144         // 格式化输出流
145         XMLWriter xmlWriter = new XMLWriter(writer, format);
146         // 将document写入到输出流
147         xmlWriter.write(document);
148         xmlWriter.close();
149         return writer.toString();
150     }
151 }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!