使用JDK原生类实现xml序列化返序列化

使用JDK原生类实现xml序列化返序列化

package com.yh.xml;

import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlSchema;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;

public class XmlDemo {

    public static void main(String[] args) throws JAXBException {
        JAXBContext jc = JAXBContext.newInstance(Domain.class);
        //复合对象
        Domain domain = new Domain();
        D d = new D();
        d.setParam1("p1");
        d.setParam2("p2");

        domain.setType("kvm");
        domain.setOs("linux");
        domain.setD(d);

        /*Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(domain, System.out);//输出到控制台*/
        System.out.println("=======序列化=======");
        String marshal = marshal(domain.getClass(), domain);
        System.out.println(marshal);
        System.out.println("=======返序列化=======");
        Domain domain1 = unmarshal(marshal, Domain.class);
        System.out.println(domain1);
    }


    /**
     * 把xml配置转换成对象
     *
     * @param xml
     * @param classObj
     * @return
     */

    public static <T> T unmarshal(String xml, Class<T> classObj) {
        T obj;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(classObj);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            obj = (T) unmarshaller.unmarshal(new StringReader(xml));
            return obj;
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 把对象转换成xml配置
     *
     * @param classObj
     * @param obj
     * @return
     */

    public static String marshal(Class<?> classObj, Object obj) {
        String xmlStr = "";
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(classObj);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //编码格式
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串
            //marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xm头声明信息
            StringWriter out = new StringWriter();
            marshaller.marshal(obj, new StreamResult(out));
            xmlStr = out.toString();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return xmlStr;
    }

}

bean 复合类

package com.yh.xml;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Domain {
    private String type;
    private String os;
    private D d;

    //xml 属性注解
    @XmlAttribute
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

    public D getD() {
        return d;
    }

    public void setD(D d) {
        this.d = d;
    }

    @Override
    public String toString() {
        return "Domain{" +
                "type="" + type + """ +
                ", os="" + os + """ +
                ", d=" + d +
                "}";
    }
}

class D {
    private String param1;
    private String param2;

    public String getParam1() {
        return param1;
    }

    public void setParam1(String param1) {
        this.param1 = param1;
    }

    public String getParam2() {
        return param2;
    }

    public void setParam2(String param2) {
        this.param2 = param2;
    }

    @Override
    public String toString() {
        return "D{" +
                "param1="" + param1 + """ +
                ", param2="" + param2 + """ +
                "}";
    }
}

package-info.java

位置必须是bean 所在同级包下才能生效

//命名空间
@XmlSchema(namespace="http://www.baidu.com", elementFormDefault = XmlNsForm.QUALIFIED)
package com.yh.xml;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

 

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 使用JDK原生类实现xml序列化返序列化