• javabean转xml


    引入pom

    <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    
    

    对应的工具类:

    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    import java.io.StringReader;
    import java.io.StringWriter;
    
    /**
     * Created by stack on 2019/4/28.
     */
    public class XmlUtil {
    
        public static Object convertXmlStrToObject(Class clazz,String xmlStr)throws Exception{
            JAXBContext context=JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller=context.createUnmarshaller();
            StringReader sr=new StringReader(xmlStr);
            return unmarshaller.unmarshal(sr);
        }
    
        /**
         *对象转换成xmlString
         *
         *createdbycaizhon2018-05-24v1.0
         */
        public static String convertToXmlStr(Object obj)throws Exception{
            //创建输出流
            StringWriter sw=new StringWriter();
    
            //利用jdk中自带的转换类实现
            JAXBContext context=JAXBContext.newInstance(obj.getClass());
    
            Marshaller marshaller=context.createMarshaller();
            //格式化xml输出的格式
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
            //去掉生成xml的默认报文头
            //marshaller.setProperty(Marshaller.JAXB_FRAGMENT,Boolean.TRUE);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            //将对象转换成输出流形式的xml
            marshaller.marshal(obj,sw);
    
            return sw.toString();
        }
    
    }
    
    

    需要使用对应的注解

    @XmlRootElement(name = “aaa”)
    @XmlElement(name = “bbb”)

    等等

    世界上所有的不公平都是由于当事人能力不足造成的.
  • 相关阅读:
    使用samba实现linux和windows文件共享
    使用li列举属性表中的某一属性
    popuptemplate的使用
    html中自动分配界面
    div中移除和添加元素
    使用v-html绑定数据,实现图片的动态转换
    使用js下载数据
    使用FeatureTable对FeatureLayer中的数据进行显示
    使用ant的checkboxGroup将列表信息添加为多选框,并根据多选框的转换进行操作
    arcgis api绘制多个点
  • 原文地址:https://www.cnblogs.com/javayida/p/13347006.html
Copyright © 2020-2023  润新知