java - XJC 不生成带有命名空间的@XmlElement 吗?

标签 java jaxb xsd xjc stax

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

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

我的类(class)是使用以下方法生成的:

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

我无法让 JAXB 解码器解析此数据。

 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 忽略了 namespace 。它产生:

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

代替(?)

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

我该如何解决这个问题?

最佳答案

将为您生成包含 @XmlSchema 注释的 package-info 类。由于指定了 namespace 以及等于 XmlNsForm.QUALIFIEDelementFormDefault 所有与未指定命名空间参数的 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;

了解更多信息

关于java - XJC 不生成带有命名空间的@XmlElement 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17789157/

相关文章:

java - Android:使用 getApplication() 时陷入 ClassCastException,尝试集成 Facebook Friendpicker 示例

java - Android 上的 OpenGL : Any conflicts when calling OpenGL functions in both Java and C++?

java - 使用 JAXB 的多个 XML 文件

java - android java设置修复微调器的默认值

java - 将具有负标度的 BigDecimal 转换为正标度

java - WSDL 中忽略的 JAX-B 全局绑定(bind)

java - JAXB - 如何使用 <set varname = "String"value = name "String2"> 元素从对象创建 xml

java - 如何解决“描述必须仅使用 WSDL "import"语句”来导入另一个 WSDL?

java - 嵌套时递归 XML 架构类型验证失败

web-services - 为什么我的 Web 服务中的元素 'type' 中的 'element' 属性值无效?