object、xml相互转换

obj转xml
package com.allianity.common.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.XStreamException;
import com.thoughtworks.xstream.io.naming.NoNameCoder;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.Xpp3Driver;

import java.io.StringWriter;

/**
* @program: workweixin-dev
* @ClassName InsureQueryXmlUtil
* @description:
* @author: Marlo
* @create: 2021-05-27 09:45
* @Version 1.0
**/
public class ToXmlUtil {
public static String toXml(Object obj) {
if (obj == null)
return null;
XStream xstream;
xstream = new XStream(new DomDriver("UTF-8"));
xstream.autodetectAnnotations(true);
xstream.processAnnotations(obj.getClass());
StringWriter writer = new StringWriter();
xstream.toXML(obj, writer);
return "<?xml version=‘1.0‘ encoding=‘GBK‘?>\n" + writer.toString().replaceAll("__", "_");
}

public static String toNoNameXml(Object obj) {
if (obj == null)
return null;
XStream xstream;
xstream = new XStream(new Xpp3Driver(new NoNameCoder()));
xstream.autodetectAnnotations(true);
xstream.processAnnotations(obj.getClass());
StringWriter writer = new StringWriter();
xstream.toXML(obj, writer);
return "<?xml version=‘1.0‘ encoding=‘GBK‘?>\n" + writer.toString();
}

public static String toXmlUTF(Object obj) {
if (obj == null)
return null;
XStream xstream;
xstream = new XStream(new DomDriver("UTF-8"));
xstream.autodetectAnnotations(true);
xstream.processAnnotations(obj.getClass());
StringWriter writer = new StringWriter();
xstream.toXML(obj, writer);
return "<?xml version=‘1.0‘ encoding=‘UTF-8‘?>\n" + writer.toString();
}

/**
* @param <T>
* @param rawxml
* @param cls
* @returnn
*/
@SuppressWarnings("unchecked")
public static <T> T toBean(String rawxml, Class<T> cls) throws XStreamException {
if (rawxml == null || "".equals(rawxml))
return null;
XStream xstream;
xstream = new XStream(new DomDriver("UTF-8"));
XStream.setupDefaultSecurity(xstream);
xstream.allowTypes(new Class[]{cls});
xstream.autodetectAnnotations(true);
xstream.processAnnotations(cls);
xstream.ignoreUnknownElements();
T obj = (T) xstream.fromXML(rawxml);
return obj;
}
}

注:若xml参数名称出现路径问题,在类外层添加
@XStreamAlias("AgentInfo")注解即可

xml转obj
package com.gllic.workweixin.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.XStreamException;
import com.thoughtworks.xstream.io.naming.NoNameCoder;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.Xpp3Driver;

import java.io.StringWriter;

/**
* @program: workweixin-dev
* @ClassName InsureQueryXmlUtil
* @description:
* @author: Marlo
* @create: 2021-05-27 09:45
* @Version 1.0
**/
public class InsureQueryXmlUtil {
public static String toXml(Object obj) {
if (obj == null)
return null;
XStream xstream;
xstream = new XStream(new DomDriver("UTF-8"));
xstream.autodetectAnnotations(true);
xstream.processAnnotations(obj.getClass());
StringWriter writer = new StringWriter();
xstream.toXML(obj, writer);
return "<?xml version=‘1.0‘ encoding=‘GBK‘?>\n" + writer.toString().replaceAll("__", "_");
}

public static String toNoNameXml(Object obj) {
if (obj == null)
return null;
XStream xstream;
xstream = new XStream(new Xpp3Driver(new NoNameCoder()));
xstream.autodetectAnnotations(true);
xstream.processAnnotations(obj.getClass());
StringWriter writer = new StringWriter();
xstream.toXML(obj, writer);
return "<?xml version=‘1.0‘ encoding=‘GBK‘?>\n" + writer.toString();
}

public static String toXmlUTF(Object obj) {
if (obj == null)
return null;
XStream xstream;
xstream = new XStream(new DomDriver("UTF-8"));
xstream.autodetectAnnotations(true);
xstream.processAnnotations(obj.getClass());
StringWriter writer = new StringWriter();
xstream.toXML(obj, writer);
return "<?xml version=‘1.0‘ encoding=‘UTF-8‘?>\n" + writer.toString();
}

/**
* @param <T>
* @param rawxml
* @param cls
* @returnn
*/
@SuppressWarnings("unchecked")
public static <T> T toBean(String rawxml, Class<T> cls) throws XStreamException {
if (rawxml == null || "".equals(rawxml))
return null;
XStream xstream;
xstream = new XStream(new DomDriver("UTF-8"));
XStream.setupDefaultSecurity(xstream);
xstream.allowTypes(new Class[]{cls});
xstream.autodetectAnnotations(true);
xstream.processAnnotations(cls);
xstream.ignoreUnknownElements();
T obj = (T) xstream.fromXML(rawxml);
return obj;
}
}


ContListTranData td = InsureQueryXmlUtil.toBean(respXML, ContListTranData.class);
 

object、xml相互转换

上一篇:echarts 多条数据渐变缩放多状态折线图


下一篇:privoxy代理服务器配置