If I have a simple class such as:-
@XmlRootElement public class MyClass { @XmlAttribute(required=true) private String myattribute }
Is it possible to validate a corresponding xml document WITHOUT an xml schema i.e. using only the annotations?
To turn off the schema validation you should set the schema-validation-enabled property to false .
Ignore XML Attribute. You can specify ignore="true" or ignore="false". The default value is false. Specifying ignore="false" has no effect on the attribute value assigned when an object of the type specified in the rule is created and no effect on the constraints.
xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.
Good question. As far as I know, the required
attribute is generated by XJC when it finds a non-optional schema type, and I think it's also used by the schema generator. At runtime, though, it's not used for anything, serving no other purpose than a documentary annotation.
One thing you could consider is the JAXB runtime's callback options. In this case, you could just define a afterUnmarshal()
method on MyClass
which programmatically validates the state of the object, throwing an exception if it doesn't like it. See the above link for other options, including registering separate validator classes.
Having said that, validation against a schema really is the best way. If you don't have one, you should considering writing one. The schemagen tool can generate a schema from your object model, which you can then modify to add whatever constraints you like. Hopefully, schemagen
will generate mandatory schema elements from your required=true
class fields.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With