XJC doesn't generate the @XmlElement with namespace?
Asked Answered
S

1

7

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 ?

Salamis answered 22/7, 2013 at 13:42 Comment(0)
Y
9

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

Yet answered 22/7, 2013 at 14:2 Comment(2)
Thank you Blaise, package-info.java was missing int my ant path. I didn't imagine that this file would be compiled.Salamis
Hi, I have a very similar problem. What exactly did you have to do to make it work? I do have the generated package-info class, but I'm not sure how to "use" it. Do I have to add it to my maven build process somehow?Kinser

© 2022 - 2024 — McMap. All rights reserved.