Down下面我有一个方法来处理给定Serializable事件的编组。我想要实现的是获取此方法的String结果,然后重新创建或重建事件对象,我不知道如果这可以是使用Unmarshaller完成。你能不能请求
private String getTextOutOfSerializable(Serializable event) {
String myXmlString = StringUtils.EMPTY;
try {
StringWriter stringWriter = new StringWriter();
Marshaller marshaller = null;
marshaller = new Marshaller(stringWriter);
marshaller.setValidation(false);
marshaller.marshal(event);
myXmlString = stringWriter.toString();
}catch(IOException e1){
LOGGER.error(" getTextOutOfSerializable IOException ",e1);
}catch(MarshalException e2){
LOGGER.error(" getTextOutOfSerializable MarshalException ",e2);
}catch(ValidationException e3){
LOGGER.error(" getTextOutOfSerializable ValidationException ",e3);
}
return myXmlString;
}
答案 0 :(得分:0)
您可以尝试使用JAXB。致Marshal:
StringWriter sw = new StringWriter();
JAXB.marshal(javaObject, sw);
对于解组使用:
JavaObject unmarshal = JAXB.unmarshal(new StringReader(string), JavObject.class);
System.out.println(unmarshal.property);