JAXB exception messages: how to change the language?
While validating XML in an XSD file using JAXB, I get JAXBExceptions
in case of invalid XML files. I am getting an exception message by calling event.getMessage()
. The resulting string is in German.
I am using JAXB 2.2. with java 8 on german system.
What determines the language of JAXB exception messages and how can I change it to English?
Here's the code:
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
Schema schema = schemaFactory.newSchema();
JAXBContext jaxbContext = JAXBContext.newInstance("myPackage");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new XMLValidationEventHandler());
unmarshaller.setSchema(schema);
myClass = (myClass) unmarshaller.unmarshal(new File("myFile.xml"));
} catch (SAXException saxE) {
System.out.println("SAX-Exception during creation of Schema object!");
saxE.printStackTrace();
} catch (JAXBException e) {}
And this is the event handler:
public class XMLValidationEventHandler implements ValidationEventHandler {
@Override
public boolean handleEvent(ValidationEvent event) {
System.out.println("XML validation failure in line "
+ event.getLocator().getLineNumber()
+ ", column " + event.getLocator().getColumnNumber()
+ ": " + event.getMessage());
return true;
}
Below is an example of event handler output:
XML validation failure in line 8, column 48: cvc-maxInclusive-valid: Wert "10000" ist nicht Facet-gültig in Bezug auf maxInclusive "8.0E3" für Typ "Type". XML validation failure in line 17, column 64: Ungültiger Wert 250 für Feld Day. XML validation failure in line 17, column 64: cvc-datatype-valid.1.2.1: "2014-02-2501: 00: 00Z" ist kein gültiger Wert für "dateTime".
source to share
OK, I found out that the language used for JAXB event messages is defined using a java system property user.language
. Therefore, the default language depends on the system on which it is running.
I changed the launch configuration of my java program by adding -Duser.language=en
. This changed the language of JAXB event messages to English.
source to share