XStream与JAXB对比
时间:序列化和反序列化1000000次。
类型 | 序列化(ms) | 反序列化(ms) |
XStraem | 90148 | 135878 |
Toplink JAXB | 34872 | 56557 |
结果对比:数据量XStream占优势,时间性能上Toplink Jaxb占明显优势
传送门查看使用XStream:https://blog.csdn.net/qq_39234840/article/details/96135995
由于JAXB是jdk知道的jar,所以无需引用第三方jar包
获取到的xml
<?xml version="1.0" encoding="GBK" standalone="yes"?>
<Data>
<Head>
<occ01>1</occ01>
<occ02>内部客户</occ02>
<occ18>内部客户</occ18>
<occacti>Y</occacti>
<occdate>2019-04-11</occdate>
</Head>
<Head>
<occ01>2</occ01>
<occ02>临时客户</occ02>
<occ18>临时客户</occ18>
<occacti>Y</occacti>
<occdate>2019-04-11</occdate>
</Head>
<Head>
<occ01>3</occ01>
<occ02>其他个人</occ02>
<occ18>其他个人</occ18>
<occacti>Y</occacti>
<occdate>2019-04-30</occdate>
</Head>
</Data>
实体类
@Data
@XmlRootElement(name = "Head")//设置根节点
@XmlAccessorType(XmlAccessType.FIELD)
public class TestXmlClass {
private String occ01;
private String occ02;
private String occ18;
private String occacti;
private String occdate;
}
@Data
@XmlRootElement(name = "Data")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataList {
@XmlElement(name = "Head")
private List<TestXmlClass> list;
}
XML转Obj工具类
package yui.bss.wx.utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.MessageFormat;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.junit.Test;
/**
* @Auth0r cYang on 2019/7/17.
*/
public class JaxbReadXmlUtil {
@SuppressWarnings("unchecked")
public static <T> T readString(Class<T> clazz, String context) throws JAXBException, IOException {
try {
// InputStream inputStream = ClassLoader.getSystemResource(context).openStream();// 本地系统路径
InputStream inputStream = new URL(context).openStream();// Http开头的线上路径
JAXBContext jc = JAXBContext.newInstance(clazz);
Unmarshaller u = jc.createUnmarshaller();
return (T) u.unmarshal(inputStream);
} catch (JAXBException e) {
throw e;
}
}
@SuppressWarnings("unchecked")
public static <T> T readConfig(Class<T> clazz, String config, Object... arguments) throws IOException,
JAXBException {
InputStream is = null;
try {
if (arguments.length > 0) {
config = MessageFormat.format(config, arguments);
}
// logger.trace("read configFileName=" + config);
JAXBContext jc = JAXBContext.newInstance(clazz);
Unmarshaller u = jc.createUnmarshaller();
is = new FileInputStream(config);
return (T) u.unmarshal(is);
} catch (IOException e) {
// logger.trace(config, e);
throw e;
} catch (JAXBException e) {
// logger.trace(config, e);
throw e;
} finally {
if (is != null) {
is.close();
}
}
}
@SuppressWarnings("unchecked")
public static <T> T readConfigFromStream(Class<T> clazz, InputStream dataStream) throws JAXBException {
try {
JAXBContext jc = JAXBContext.newInstance(clazz);
Unmarshaller u = jc.createUnmarshaller();
return (T) u.unmarshal(dataStream);
} catch (JAXBException e) {
throw e;
}
}
}
XML转Obj实现代码
@Test
public void testXmlToObj() throws JAXBException, IOException{
// 一句代码搞定
DataList dataList = JaxbReadXmlUtil.readString(DataList.class, "http://192.168.0.220/cgi-bin/fglccgi/topprod/tiptop/out/CUSTOMER12019071709223538200.xml");
}
Obj转XML工具类
public class JaxbWriteXmlUtil {
public static <T> Marshaller getMarshaller(Class<T> c) throws JAXBException{
JAXBContext context = JAXBContext.newInstance(c);
Marshaller marshaller = context.createMarshaller();
//编码格式
marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
//是否格式化生成的xml串
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//是否省略xml头信息(<?xml version="1.0" encoding="gb2312" standalone="yes"?>)
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
return marshaller;
}
}
Obj转XML实现代码
@Test
public void testObjToXml1() throws JAXBException{
Marshaller marshaller = JaxbWriteXmlUtil.getMarshaller(People.class);
People people = new People("001","测试",22);
marshaller.marshal(people, System.out);
}
@Test
public void testObjToXml2() throws JAXBException{
Marshaller marshaller = JaxbWriteXmlUtil.getMarshaller(ObjList.class);
ArrayList<TestObj> list = new ArrayList<TestObj>();
TestObj testObj1 = new TestObj(002, "测试", new Date());
TestObj testObj2 = new TestObj(003, "测试", new Date());
list.add(testObj1);
list.add(testObj2);
ObjList objlist = new ObjList();
objlist.setObjList(list);
marshaller.marshal(objlist, System.out);
}