aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
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
Author

How can i remove namespace from the generated JAXB File ?

Dupont Girard
Greenhorn

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



Build.xml:

binding.xml:



And finnaly



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

It didn't work correctly because i got this:@XmlRootElement(name = "Player", namespace = "http://www.example.org/Player")

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

How can i remove this "namespace" ?

Thanks
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 535
    
    3
[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 package-info.java. 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.
 
Don't get me started about those stupid light bulbs.
 
subject: How can i remove namespace from the generated JAXB File ?