• JAXB java对象和xml互转


     /**
         * 将对象转为流程XML
         */
        public static String objtoxml(Object obj) {
            // 创建输出流
            StringWriter sw = new StringWriter();
            try {
                // 利用jdk中自带的转换类实现
                JAXBContext context = JAXBContext.newInstance(obj.getClass());
    
                Marshaller marshaller = context.createMarshaller();
                // 格式化xml输出的格式
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                        Boolean.TRUE);
                // 将对象转换成输出流形式的xml
                marshaller.marshal(obj, sw);
            } catch (JAXBException e) {
                e.printStackTrace();
            }
            return sw.toString();
        }
    /**
         * xml转对象
         */
        public static Object xmltoobj(Class clazz, String xmlStr) {
            Object xmlObject = null;
            try {
                JAXBContext context = JAXBContext.newInstance(clazz);
                // 进行将Xml转成对象的核心接口
                Unmarshaller unmarshaller = context.createUnmarshaller();
                StringReader sr = new StringReader(xmlStr);
                xmlObject = unmarshaller.unmarshal(sr);
            } catch (JAXBException e) {
                e.printStackTrace();
            }
            return xmlObject;
        }
  • 相关阅读:
    free
    wc
    awk
    wall
    sed
    Genymotion常见问题整合与解决方案
    Genymotion常见问题整合与解决方案
    java.net.MalformedURLException 异常
    java.net.MalformedURLException 异常
    异常:android.os.NetworkOnMainThreadException
  • 原文地址:https://www.cnblogs.com/yang1018/p/11751033.html
Copyright © 2020-2023  润新知