From 42c67add8090aa4779dd1df10422bf7fc34b8038 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Thu, 19 Dec 2013 17:33:02 +0800 Subject: [PATCH] edit marshalHelper --- .../bench4q/share/helper/MarshalHelper.java | 69 +++++++++---------- 1 file changed, 31 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/bench4q/share/helper/MarshalHelper.java b/src/main/java/org/bench4q/share/helper/MarshalHelper.java index cc703ba3..c9c4ce05 100644 --- a/src/main/java/org/bench4q/share/helper/MarshalHelper.java +++ b/src/main/java/org/bench4q/share/helper/MarshalHelper.java @@ -1,38 +1,31 @@ -package org.bench4q.share.helper; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; - -public class MarshalHelper { - - public static String marshal(Class classToMarshal, Object input) { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - Marshaller marshaller; - try { - marshaller = JAXBContext.newInstance(classToMarshal) - .createMarshaller(); - marshaller.marshal(input, os); - } catch (JAXBException e) { - e.printStackTrace(); - } - return os.toString(); - } - - public static Object unmarshal(Class classToUnmarshal, String input) { - Unmarshaller unmarshaller; - try { - unmarshaller = JAXBContext.newInstance(classToUnmarshal) - .createUnmarshaller(); - return unmarshaller.unmarshal(new ByteArrayInputStream(input - .getBytes())); - } catch (JAXBException e) { - e.printStackTrace(); - return null; - } - } -} +package org.bench4q.share.helper; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; + +public class MarshalHelper { + + public static String marshal(Class classToMarshal, Object input) + throws JAXBException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + Marshaller marshaller; + marshaller = JAXBContext.newInstance(classToMarshal).createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + marshaller.marshal(input, os); + return os.toString(); + } + + public static Object unmarshal(Class classToUnmarshal, String input) + throws JAXBException { + Unmarshaller unmarshaller; + unmarshaller = JAXBContext.newInstance(classToUnmarshal) + .createUnmarshaller(); + return unmarshaller + .unmarshal(new ByteArrayInputStream(input.getBytes())); + } +}