欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

对象与xml相互转换

程序员文章站 2024-03-16 09:44:40
...

model对象

package model;

import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "service")
public class XmlModel {
    @XmlElement
    private String data;
    @XmlElement
    private List<ContentElement> content;
    @XmlElement
    private String test;
    public String getData() {
        return data;
    }
    public void setData(String data) {
        this.data = data;
    }

    public List<ContentElement> getContent() {
        return content;
    }
    public void setContent(List<ContentElement> content) {
        this.content = content;
    }

    public String getTest() {
        return test;
    }
    public void setTest(String test) {
        this.test = test;
    }
    @Override
    public String toString() {
        return "XmlModel [data=" + data + ", content=" + content + ", test=" + test + "]";
    }
}



package model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "content")
public class ContentElement {
    @XmlElement(name = "content1")
    private String content1;
    @XmlElement(name = "content2")
    private String content2;
    @XmlElement(name = "content3")
    private String content3;
    public String getContent1() {
        return content1;
    }
    public void setContent1(String content1) {
        this.content1 = content1;
    }
    public String getContent2() {
        return content2;
    }
    public void setContent2(String content2) {
        this.content2 = content2;
    }
    public String getContent3() {
        return content3;
    }
    public void setContent3(String content3) {
        this.content3 = content3;
    }
    @Override
    public String toString() {
        return "ContentElement [content1=" + content1 + ", content2=" + content2 + ", content3=" + content3 + "]";
    }

}

对象与xml相互转换工具类

package com.jx.aisino.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public abstract class JaxbUtils {
    public static String getXmlByObject(Object object) {
        String dataXml = "";
        try {
            if (object != null) {
                JAXBContext cxt = JAXBContext.newInstance(object.getClass());
                Marshaller marshaller = cxt.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 编码格式
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);// 是否格式化生成的xml串
                marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);// 默认false表示xml指令存在

                StringWriter sw = new StringWriter();
                marshaller.marshal(object, sw);
                dataXml = sw.toString();
            }
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return dataXml;
    }


    public static Object toPojo(String xml, Class<?> clazz) throws Exception {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(
                xml.getBytes("UTF-8"));
        return JaxbUtils.toPojo(inputStream, clazz);
    }

    public static Object toPojo(InputStream in, Class<?> clazz)
            throws Exception {
        JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
        Unmarshaller u = jaxbContext.createUnmarshaller();
        return u.unmarshal(in);
    }

}

测试类

package test;

import java.util.ArrayList;
import java.util.List;

import com.jx.aisino.util.JaxbUtils;

import model.ContentElement;
import model.XmlModel;

public class TestJaxbUtils {

    public static void main(String[] args) {
try {
            //对象转换xml
            XmlModel model = new XmlModel();
            ContentElement contentElement = new ContentElement();
            contentElement.setContent1("66+46546");
            contentElement.setContent2("232131");
            model.setData("测试");
            List<ContentElement> cList = new ArrayList<>();
            cList.add(contentElement);
            model.setContent(cList);
            model.setTest(" 2132132 ");
            String reqs = JaxbUtils.getXmlByObject(model);
            System.out.println("请求报文:"+reqs);
            //xml转换对象
            XmlModel model2 = (XmlModel)JaxbUtils.toPojo(reqs, XmlModel.class);
            System.out.println(model2.toString());
        }catch(Exception exception){
            System.out.println("出错");
        }

    }

}
相关标签: xml