XJC doesn't generate the @XmlElement with namespace?

Pierre picture Pierre · Jul 22, 2013 · Viewed 8.1k times · Source

I want to parse data using JAXB for the the following XSD schema http://www.uniprot.org/support/docs/uniprot.xsd .

A typical XML for this looks like this: http://www.uniprot.org/uniprot/Q8NEJ9.xml

My Classes were generated using:

xjc http://www.uniprot.org/support/docs/uniprot.xsd

I cannot get a JAXB unmarshaller to parse this data.

 xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
  XMLEventReader rx=xmlInputFactory.createXMLEventReader(in);
  final QName uEntry=new QName("http://uniprot.org/uniprot","entry");

  while(rx.hasNext())
    {
    XMLEvent evt=rx.peek();
    if(!(evt.isStartElement() && evt.asStartElement().getName().equals(uEntry)))
      {
      rx.next();
      continue;
      }
    JAXBElement<Entry> jaxbElement=uniprotUnmarshaller.unmarshal(rx, Entry.class);
    Entry entry= jaxbElement.getValue();
    (...) 
   }

Each instance of 'entry' remains empty. When an entry is marshaled to stderr, I get something like:

<ns2:entry xmlns:ns2="http://uniprot.org/uniprot" dataset="Swiss-Prot" created="2011-06-28+01:00" modified="2011-09-21+01:00" version="20"/>

I think it's because xjc ignores the namespaces. It generates:

@XmlRootElement(name = "entry")
public class Entry {

instead of (?)

@XmlRootElement(name = "entry",namespace="http://uniprot.org/uniprot")
public class Entry {

How can I fix this ?

Answer

bdoughan picture bdoughan · Jul 22, 2013

A package-info class containing the @XmlSchema annotation will be generated for you. Since a namespace was specified along with elementFormDefault equal to XmlNsForm.QUALIFIED all annotations corresponding to XML elements without a namespace parameter specified will belong to this namespace.

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2013.07.22 at 10:14:54 AM EDT 
//

@javax.xml.bind.annotation.XmlSchema(namespace = "http://uniprot.org/uniprot", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.uniprot.uniprot;

For More Information