File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes How can i remove namespace from the generated JAXB File ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How can i remove namespace from the generated JAXB File ?" Watch "How can i remove namespace from the generated JAXB File ?" New topic

How can i remove namespace from the generated JAXB File ?

Dupont Girard

Joined: Mar 21, 2011
Posts: 24
Hello, here is my code:



And finnaly

i add "xjc:simple" in order to have @XMLRootElement, but an exception is raised: javax.xml.bind.UnmarshalException: unexpected element (uri:""

It didn't work correctly because i got this:@XmlRootElement(name = "Player", namespace = "")

Instead of just: @XmlRootElement(name = "Player")

How can i remove this "namespace" ?

g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 632
[1] One thing you can try is to take out the external binding and make sure you do not use -npa switch for xjc. Then the schema will be compiled with namespace info contained in You can then manually delete or rename it with an extension other than .java. After doing that, I think you can safely unmarshal and marshal an xml document belonging to no namespace, as long as you don't validate it with the original schema.

[2] But what I don't understand the business logic here is that if you can do thing like [1], you can as well just retouch the schema(s) by eliminating the targetNamespace(s) and its ramifications for the purpose of adapting to the changing requirement in the sense that you now have to deal with incoming doc without namespace rather than having one originally.

[3] Given the assumption that you don't really want to touch the schema files and you don't want neither to do anything on the generated source .java files and nevertheless want to deal with the no namespace case, you can do it like this. You can write an XMLFilter implementation, mapping the startElement from no namespace to having the namespace coincident with the targetNamespace in the schema(s)... Make it stand between the sax reader and streaming to the unmarshaller. In that case all the addition measures are taken place in the application level leaving the compilation package and schema intact. This would be a cleaner approach in case [1] or [2] is not an option as they make modification to the package related to the schema compilation. This approach should work but I confess that it does sound a bit abstract until one makes their hands dirty in implementing it which is not overwhelmingly complicated for this shown simple document structure.
I agree. Here's the link:
subject: How can i remove namespace from the generated JAXB File ?
jQuery in Action, 3rd edition