XmlRootElement的作用和用法

手动封装实体类转换成xml

实体类

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @ClassName RequetsXml
 * @Description TODO
 * @Author xiongchao
 * @Date 2020/7/14 10:23
 **/

@XmlRootElement(name = "request")
public class RequetsXml {

    @XmlElement(name = "casefile")
    private CaseFile caseFile;

    @XmlElement(name = "uploaded-file-id")
    private String fileId;


    public void setCaseFile(CaseFile caseFile) {
        this.caseFile = caseFile;
    }

    public void setFileId(String fileId) {
        this.fileId = fileId;
    }

    private static class CaseFile{

        private String name;

        @XmlElement(name = "isTmpFile")
        private boolean isTmpFile;

        @XmlElement(name = "modify-timestamp")
        private String modifyTimStamp;

        @XmlElement(name = "sdb-group-id")
        private Integer groupId;

        @XmlElement(name = "source-system-id")
        private Integer sourceId;



        public void setName(String name) {
            this.name = name;
        }


        public void setTmpFile(boolean tmpFile) {
            isTmpFile = tmpFile;
        }


        public void setModifyTimStamp(String modifyTimStamp) {
            this.modifyTimStamp = modifyTimStamp;
        }

        public void setGroupId(Integer groupId) {
            this.groupId = groupId;
        }

        public void setSourceId(Integer sourceId) {
            this.sourceId = sourceId;
        }
    }

测试

public static void main(String[] args) throws JAXBException {
        RequetsXml root = new RequetsXml();
        root.setFileId("adsa45475454");

        CaseFile caseFile = new CaseFile();
        caseFile.setName("王凯");
        caseFile.setTmpFile(false);
        caseFile.setGroupId(1);
        caseFile.setSourceId(0);
        caseFile.setModifyTimStamp("1575454212");
        root.setCaseFile(caseFile);

        JAXBContext context = JAXBContext.newInstance(RequetsXml.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(root, System.out);

    }

输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request>
    <casefile>
        <name>王凯</name>
        <isTmpFile>false</isTmpFile>
        <modify-timestamp>1575454212</modify-timestamp>
        <sdb-group-id>1</sdb-group-id>
        <source-system-id>0</source-system-id>
    </casefile>
    <uploaded-file-id>adsa45475454</uploaded-file-id>
</request>

工具类封装

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.json.JSONException;
import org.json.XML;

import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 *
 * Class Name: XmlConvertUtil
 * Description: xml格式转化工具类
 *
 */
public class XmlConvertUtil {
	/**
	 * xml转换成JavaBean
	 *
	 * @param xml xml格式字符串
	 * @param t 待转化的对象
	 * @return 转化后的对象
	 * @throws Exception JAXBException
	 */
	@SuppressWarnings({ "unchecked" })
	public static <T> T convertToJavaBean(String xml, Class<T> t) throws Exception {
		T obj = null;
		JAXBContext context = JAXBContext.newInstance(t);
		Unmarshaller unmarshaller = context.createUnmarshaller();
		obj = (T) unmarshaller.unmarshal(new StringReader(xml));
		return obj;
	}

	/**
	 * JavaBean转换成xml
	 * @param obj
	 * @param encoding
	 * @return
	 */
	public static String convertToXml(Object obj, String encoding) {
		String result = null;
		try {
			JAXBContext context = JAXBContext.newInstance(obj.getClass());
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

			StringWriter writer = new StringWriter();
			marshaller.marshal(obj, writer);
			result = writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public static JSONObject xmlToJson(String xml) {
		try {
			org.json.JSONObject object = XML.toJSONObject(xml);
			String jsonData = object.get("response").toString();
			JSONObject jsonObject = JSON.parseObject(jsonData);
			return jsonObject;
			/*JSONObject result = (JSONObject) jsonObject.get("result");
			JSONObject upload = (JSONObject) result.get("upload");
			String uploadUrl = upload.get("url").toString();
			String fildId = upload.get("upload-file-id").toString();*/
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
}

切记,实体类中的@XmlRootElement注解不可一省略,否则会抛异常

[特别重要] 补充一下,加上get方法之后,如果出现报错,在get方法上面添加注解@XmlTransient

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值