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);
}