I have to marshall large amount of data into XML format. I am looking into JAXB since it is part of JDK 8 but I am not sure about how it performs with huge data. Jackson XML is another library I come across with which is newer. Is Jackson faster than JAXB in serializing object to XML?
I once wrote an use case to test the performace of jackson and jaxb in serializing and deserializing object to/from xml. test code:
serializing:
/** counter */
private int counter = 10000;
@Test
public void doTest() throws JAXBException{
TextMsg msg = new TextMsg();
msg.setToUserName("jackson");
msg.setFromUserName("hawaii");
msg.setContent("jack<xml val='Json'>]]>");
long start = System.currentTimeMillis();
for(int i=0; i< counter; i++){
ByteArrayOutputStream xmlOut = null;
ByteArrayInputStream xmlIn = null;
try{
xmlOut = new ByteArrayOutputStream();
XMLFactory.toXML(msg, xmlOut);
String xml = new String(xmlOut.toByteArray());
}finally{
IOUtils.closeQuietly(xmlIn);
IOUtils.closeQuietly(xmlOut);
}
}
long end = System.currentTimeMillis();
logger.info("consume:{}", end - start);
}
deserializing
/** counter */
private int counter = 10000;
@Test
public void doTest() throws IOException, JAXBException{
String xml = "<?xml version='1.0' encoding='UTF-8'?><xml><ToUserName><![CDATA[jackson]]></ToUserName><FromUserName><![CDATA[hawaii]]></FromUserName><Content><![CDATA[jack<xml val='Json'>]]>]]></Content></xml>";
long start = System.currentTimeMillis();
for(int i=0; i< counter; i++){
ByteArrayOutputStream xmlOut = null;
ByteArrayInputStream xmlIn = null;
try{
xmlOut = new ByteArrayOutputStream();
TextMsg textMsg = XMLFactory.fromXML(xml, TextMsg.class);
}finally{
IOUtils.closeQuietly(xmlIn);
IOUtils.closeQuietly(xmlOut);
}
}
long end = System.currentTimeMillis();
logger.info("consume:{}", end - start);
}
result(unit: millisecond, avg for 3 times test):
type: entity -> xml
JAXB:24716
Jackson:1123
JAXB:Jackson 22:1
type: xml -> entity
JAXB: 31622
Jackson: 1049
JAXB:Jackson 30:1
conclusion: The same task, In serializing, Jackson uses 1/22 time-consume of JAXB. In deserializing, Jackson uses 1/30 time-consume of JAXB.