I encountered a situation where a javax.xml.bind.Marshaller produces not well-formed XML, despite throwing no exception nor raising a validation event. The problem arises when using @XmlAnyAttribute-Annotation in a special way.
Consider the following scenario (imports omitted):
The invocation of Main.main does the following:
- No exception is raised.
- The handleEvent-method is not called.
- A file "test.xml" is written, in which the element "test" carries the attribute "id" twice. Once with value "5", and once with value "10". Hence, it is not well-formed XML.
Since the operation silently succeeds, a user expects a valid XML with respect to the XML schema. As far as I know, validity w.r.t. schema implies that it is also well-formed. Assuming no schema is used, a user would expect that the output is well-formed if no error or warning is given.
The ugly workaround is to add an additional check on top of the Marshal validation for well-formedness, as long as this shortcoming exists.
I am aware of the fact that this usage of the XmlAnyAttribute-map can be considered as an edge case. Nevertheless, the Marshaller should check for validity of the input. At the moment, the silent-fail-outcome is highly undesirable.
Proposals in which direction a fix could go:
- A Marshaller raises an exception if the output would not be well-formed XML. The postcondition, that the output is always well-formed, should be included in the javadocs.
- In the special case where a regularly mapped attribute is again specified in the XmlAnyAttribute-map, this error is explained in the exception.
- If proposal 1 can not be implemented: When using a XML schema on a Marshaller, the validation succeeds only if the XML is also well-formed.
Thanks for looking into this!