Castor and the Namespace bug
While working with a new version of Castor I recently encountered a strange error during unmarshalling (creating Java object out of corresponding XML schema). The error was as follows:
java.lang.IllegalArgumentException: The prefix ‘xml’ is reserved (XML 1.0 Specification) and cannot be declared.
Luckily, some Googling brought me to this thread which explains the probable reasons for this “bug”. As it suggested (and it works since I incorporated it) we need to set the namespaces property to true in the castor.properties file as follows (caveat: needs to be done with Xerces 2.5 only):
[code lang=”java”]org.exolab.castor.parser.namespaces = true[/code]
Following is a quote from the said thread, which is in fact a reply from Keith Visco, the Castor XML project lead, that throws light on the cause of the bug:
The issue seems to be with newer versions of Xerces. The older version that ships with Castor works fine. For some reason, when the newer version of Xerces encounters an “xml” prefixed attribute, such as “xml:lang” it tries to automatically start a prefix mapping for “xml”. Which, in my opinion, is technically incorrect. They shouldn’t be doing that. According to the w3c, the “xml” prefix should never be declared.
The reason it started appearing in the new Castor (0.9.5.2), was because of a switch to SAX 2 by default during unmarshalling. I found a simple workaround (tested with Xerces 2.5) , at first I thought about disabling namespace processing in Xerces, but then realized that it’s already disabled by default by Castor…so I have no idea why they call #startPrefixMapping when namespace processing has been disabled. But in any event…explicitly enabling namespace processing seems to fix the problem.