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>falseisTmpFile>
        <modify-timestamp>1575454212modify-timestamp>
        <sdb-group-id>1sdb-group-id>
        <source-system-id>0source-system-id>
    casefile>
    <uploaded-file-id>adsa45475454uploaded-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 convertToJavaBean(String xml, Class 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

你可能感兴趣的:(json,xml,typescript,mybatis,java)