本文整理汇总了Java中javax.xml.bind.Marshaller.setEventHandler方法的典型用法代码示例。如果您正苦于以下问题:Java Marshaller.setEventHandler方法的具体用法?Java Marshaller.setEventHandler怎么用?Java Marshaller.setEventHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.bind.Marshaller
的用法示例。
在下文中一共展示了Marshaller.setEventHandler方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: marshallToXml
import javax.xml.bind.Marshaller; //导入方法依赖的package包/类
public String marshallToXml(Statistics statistics) {
try {
Marshaller marshaller = jaxbContext.createMarshaller();
StringWriter stringWriter = new StringWriter();
List<ValidationEvent> validationEvents = new ArrayList<>();
marshaller.setEventHandler(event -> {
if (event.getSeverity() == ValidationEvent.ERROR ||
event.getSeverity() == ValidationEvent.FATAL_ERROR) {
validationEvents.add(event);
}
//let the un-marshalling proceed to find any more problems
return true;
});
marshaller.marshal(statistics, stringWriter);
if (!validationEvents.isEmpty()) {
String detail = validationEventsToString(validationEvents);
throw new RuntimeException("Errors encountered marshalling xml: " + detail);
}
return stringWriter.toString();
} catch (JAXBException e) {
LOGGER.error("Error marshalling message value");
throw new RuntimeException(String.format("Error marshalling message value"), e);
}
}