XJC不会生成带命名空间的@XmlElement吗?

时间:2013-07-22 13:42:57

标签: java jaxb xsd xjc stax

我想使用JAXB为以下XSD架构http://www.uniprot.org/support/docs/uniprot.xsd解析数据。

典型的XML如下所示:http://www.uniprot.org/uniprot/Q8NEJ9.xml

我的类是使用:

生成的
xjc http://www.uniprot.org/support/docs/uniprot.xsd

我无法获得一个JAXB unmarshaller来解析这些数据。

 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();
    (...) 
   }

“条目”的每个实例都保持为空。当一个条目被封送到stderr时,我得到类似的东西:

<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"/>

我认为这是因为xjc忽略了命名空间。它会生成:

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

而不是(?)

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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:7)

将为您生成包含package-info注释的@XmlSchema类。由于指定namespace以及elementFormDefault等于XmlNsForm.QUALIFIED,因此所有未指定命名空间参数的XML元素对应的注释都属于此命名空间。

//
// 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;

了解更多信息

相关问题