edit marshalHelper

This commit is contained in:
coderfengyun 2013-12-19 17:33:02 +08:00
parent 45e9db7284
commit 42c67add80
1 changed files with 31 additions and 38 deletions

View File

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