本文整理匯總了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);
}
}