Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XJC doesn't generate the @XmlElement with namespace?

Tags:

java

jaxb

xsd

xjc

stax

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 ?

like image 692
Pierre Avatar asked Jul 22 '13 13:07

Pierre


People also ask

How do you use XJC generated classes?

Open a command prompt. Run the JAXB schema compiler, xjc command from the directory where the schema file is located. The xjc schema compiler tool is located in the app_server_root \bin\ directory. Use the generated JAXB objects within a Java application to manipulate XML content through the generated JAXB classes.

Is XJC a JAXB?

Use the Java™ Architecture for XML Binding (JAXB) tools to generate Java classes from an XML schema with the xjc schema compiler tool.

What is ObjectFactory in JAXB?

jaxb package. An ObjectFactory allows you to programatically construct new instances of the Java representation for XML content. The Java representation of XML content can consist of schema derived interfaces and classes representing the binding of schema type definitions, element declarations and model groups.


1 Answers

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

  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
like image 165
bdoughan Avatar answered Sep 22 '22 23:09

bdoughan