Quantcast

JAXB Fails to handle namespaces

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

JAXB Fails to handle namespaces

metro-3
Hello everybody,

I am struggling with JAXB & XJC to build bindings to handle COLLADA xml files. I spent a lot of time on forums and googling. It does not works which makes me mad.
 
I work with JAXB bunduled with jre1.6

COLLADA 1.4 xsd is available at http://www.khronos.org/files/collada_schema_1_4

I successfully generate java binding classes:
xjc.exe collada_schema_1_4.xsd  -b collada_schema_1_4.xjb  
collada_schema_1_4.xjb binding solve the name collision and specify a destination package.

The following javacode try to parse "scene.DAE" XML file. "scene.DAE" has been validated by external tools and JAXB unmarshall validation validaion, it conforms collada_schema_1_4.xsd perfectly.

JAXBContext jc = JAXBContext.newInstance("com.jmex.model.collada.collada_schema_1_4");
Unmarshaller u = jc.createUnmarshaller();
collada = (COLLADA) u.unmarshal(sourceStream);


Here is COLLADA root node
<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1">

The marshalled throws the following exception
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.collada.org/2005/11/COLLADASchema", local:"COLLADA"). Expected elements are <{}COLLADA>

Issue 1 - The root node fails to be unmarshalled.
Well, JAXB fails to instantiate the root XML node (which is COLLADA in the symbol space http://www.collada.org/2005/11/COLLADASchema) because JAXB's dictionary only contains COLLADA in "" symbol space.

Here is COLLADA generated bean with annotations:
@XmlRootElement(name = "COLLADA")
public class COLLADA {

It looks to be a error regarding XSD specification (http://www.w3.org/TR/xmlschema11-1/#declare-element) COLLADA xml node is validated by a top level element declaration and must be qualified by schema targetNameSpace. [b]Why does XJC not bind xsd top level elements to xsd target namespace?[/b]


I tried to update the bean file and add the namespace:
@XmlRootElement(name = "COLLADA",namespace="http://www.collada.org/2005/11/COLLADASchema")
public class COLLADA {

The unmarshall function call did not throw any exception, but the returned COLLADA bean fields are all “null”.

I dropped a breakpoint in JAXB Structure Loader: When SAX startElement event talks about "asset", namespace="http://www.collada.org/2005/11/COLLADASchema", structure loader only knows "asset" , namespace="" in it's childUnmarshallers.

xsd schema element is :
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.collada.org/2005/11/COLLADASchema" xml:lang="EN" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd" elementFormDefault="qualified" targetNamespace="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1" xmlns:xs="http://www.w3.org/2001/XMLSchema">

Maybe Issue 2 - When xsd schema elementFormDefault is "qualified", XJC should match elements with namespace. [b]Why does JAXB local elements not take into account namespace ?[/b]

Maybe Issue 3 - Changing "asset" bean annotations to add the namespace has no effect, on JAXB. [b]What is annotation "namespace" field done for ?[/b]

Provided JAXB does not take into account namesapace of xml elements, it wrote the following code in order to ignore namespace while parsing.

JAXBContext jc = JAXBContext.newInstance("com.jmex.model.collada.collada_schema_1_4");
                Unmarshaller u = jc.createUnmarshaller();
                SAXParserFactory parserFactory;
                parserFactory = SAXParserFactory.newInstance();
                parserFactory.setNamespaceAware(false);
                XMLReader reader = parserFactory.newSAXParser().getXMLReader();
                Source er = new SAXSource(reader, new InputSource(sourceStream));
                collada = (COLLADA) u.unmarshal(er);

It works, JAXB unmarshalling is successful. (I hope this piece of code can help someone...)

Now I added "collada_schema_1_4_identifier.xjb" binding customization to XJC command line in order to override de default implementations of JAXB beans with mine. On unmarshalling an error occurs.
        com.jmex.model.collada.collada_schema_1_4.Vertices cannot be cast to com.jmex.model.collada.impl.VerticesImpl
         at com.jmex.model.collada.collada_schema_1_4.Mesh$JaxbAccessorF_vertices.set(FieldAccessor_Ref.java:56)
                at com.sun.xml.bind.v2.runtime.reflect.Accessor.receive(Accessor.java:171)  

I don't know why the JAXB factory does not takes into account, my bindings. The generated ObjectFactory looks to be correct. It would created instances of my custom beans, but it is not called at all. The standard class loader is used instead. I feel like disabling namespaces is breaking the factory mechanism of JAXB.

[b]Have you any clue to help me?[/b]
[Message sent by forum member 'sensei_pedro' (sensei_pedro)]

http://forums.java.net/jive/thread.jspa?messageID=334331

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXB Fails to handle namespaces

metro-3
I have drop some breakpoints and ObjectFactory is never called. Why ?
[Message sent by forum member 'sensei_pedro' (sensei_pedro)]

http://forums.java.net/jive/thread.jspa?messageID=334649

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXB Fails to handle namespaces

metro-3
In reply to this post by metro-3
Hi sensei_pedro

First of all, thanks for you great post, it helped me with a problem I was having with jaxb. I also looked everywhere for solutions, with no luck. Until I ran into your post yesterday :)
I was having the same problem as you, getting the UnmarshallException when my root element has an xmlns defined, with everything well defined and so on.
Your code for ignoring namespaces worked for me and I'm now able to parse the xml (for now this is the most important for me), but as you said, disabling this doesn't seem very logical. What do you think is causing this? Some sort of jaxb/xjc bug?

Thanks again,
Francisco
[Message sent by forum member 'fcardoso' (fcardoso)]

http://forums.java.net/jive/thread.jspa?messageID=334796

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXB Fails to handle namespaces

Clive Brettingham-Moore-3
Well I don't know if it is your problem but it looks to me like
sensei_pedro has lost the package annotations (package-info.java
compiled to package-info.class) - JAXB generated code declares schema
namespace and element qualification here (that's why @XmlRootElement has
no explicit ns)) so losing it would produce the symptoms described.
Check that the classpath you are using contain package-info for the
generated package.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXB Fails to handle namespaces

metro-3
No, in my case the package-info was there (with xmlschema correct) and still didn't work. Btw, due to some project restrictions I have to use jaxb 2.0, maybe that's what's causing me these problems.
[Message sent by forum member 'fcardoso' (fcardoso)]

http://forums.java.net/jive/thread.jspa?messageID=334949

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXB Fails to handle namespaces

Clive Brettingham-Moore-3
Have you tried checking that the annotation is making it all the way
into the final runtime, eg

System.out.println("@XMLSchema " +
ObjectFactory.class.getPackage().getAnnotation(javax.xml.bind.annotation.XmlSchema.class));

Assuming the annotation is definitely present and correct as exposed to
JAXB then you may have a JAXB bug (if so try building a standalone test
case - in addition to helping get the bug fixed, often this will help
you find the problem if it is not a bug, or a workaround if it is).

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXB Fails to handle namespaces

metro-3
In reply to this post by Clive Brettingham-Moore-3
Thanks for your response.
I checked the generated sources and the file ‘package-info.class’ is correct. I have regenerated the code several times (taking care about clearing the content of target directories) and leave it unchanged. The generated  .java file are definitly OK.

I was surprised to discover that the problem of namespace is unstable (I means not always reproductible). Clean code, regenerate code, then press Netbeans' run button can makes it work, then makes it crash. The mad thing is that I succeed unmarshalling the COLLADA file when running my application from command line. It looks like Netbeans mess up under the hood.

[b] fcardoso are you working with Netbeans ?[/b]

Once Netbeans fails to run my application one time, it is useless to press “Clean & Build”, the application will not succeed. There must be some cache that is not invalidated until the .java files are not changed (On netbeans side or on JRE side). I lost hours following eroneous leads because of this strange behavior.

[b]The only safe procedure is the following one:[/b]
Run netbeans "clean" command, Exit netbeans, Delete target directories, run XJC,  Start Netbeans, Run project.
Run the following code:
     JAXBContext jc = JAXBContext.newInstance("com.jmex.model.collada.collada_schema_1_4");
     Unmarshaller u = jc.createUnmarshaller();
     collada = (COLLADA) u.unmarshal(sourceStream);
[Message sent by forum member 'sensei_pedro' (sensei_pedro)]

http://forums.java.net/jive/thread.jspa?messageID=335227

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXB Fails to handle namespaces

metro-3
In reply to this post by metro-3
Check out:

   http://javagoldmine.site90.net

There are plenty of articles on JAXB with hundreds
of code snippets/examples and expert opinions
and discussions.

Mirrors:

http://javagoldmine.uuuq.com
http://javagoldmine.by.ru
http://tarkus01.by.ru
[Message sent by forum member 'elquedros' (elquedros)]

http://forums.java.net/jive/thread.jspa?messageID=335369

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXB Fails to handle namespaces

metro-3
In reply to this post by metro-3
I received a mail from Wolfgang L. with an interesting and elegant workaround for the Cast exception issue.

The trick is to get read of XML binding class implementation overloading by writing an XJC plugin.
In fact I needed the unmarshalled class to implement an interface on it's accessers (getId() and getSID()). I tried to generate XML binding derivate class and XJB binding configuration files in order to make every element with an id/sid attribute implement the interface. This approach failed because of the cast exception (maybe a bug)

Wolfgang L. provided an XJC plugin, so that the bindings generated by XJC implements the interface ( getting rid of implementation override). It works seamlessly. I attached the archive with the code and README inside. I hope it will be usefull to someone.

A issues raised by this post have been solved or bypassed.
[Message sent by forum member 'sensei_pedro' (sensei_pedro)]

http://forums.java.net/jive/thread.jspa?messageID=335828

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXB Fails to handle namespaces

metro-3
In reply to this post by metro-3
> [...]
> It looks to be a error regarding XSD specification
> (http://www.w3.org/TR/xmlschema11-1/#declare-element)
> COLLADA xml node is validated by a top level element
> declaration and must be qualified by schema
> targetNameSpace. [b]Why does XJC not bind xsd top
> level elements to xsd target namespace?[/b]
>
>
> I tried to update the bean file and add the
> namespace:
> @XmlRootElement(name =
> "COLLADA",namespace="http://www.collada.org/2005/11/CO
> LLADASchema")
> public class COLLADA {
>
> The unmarshall function call did not throw any
> exception, but the returned COLLADA bean fields are
> all “null”.
> [...]

Hi,

first of all thanks for your great post. After reading your solution I added the namespace attribute to @XmlRootElement annotation as you did above. At first the outcome looked the same for me - the bean fields were all null. But after I added an additional @XmlAccessorType(XmlAccessType.FIELD) annotation it worked well and all fields were initialized with the correct values from my XML file. Please don't ask me why the same binding is working without @XmlAccessorType annotation when namespaces are totally omitted. I guess this remains the secret of JAXB.

Regards,
Joern.
[Message sent by forum member 'joernf' (joernf)]

http://forums.java.net/jive/thread.jspa?messageID=338543

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXB Fails to handle namespaces

latif
This post has NOT been accepted by the mailing list yet.
In reply to this post by metro-3
Thanks man , you save my day
Loading...