【问题标题】:XJC doesn't generate the @XmlElement with namespace?XJC 不生成带有命名空间的@XmlElement?
【发布时间】:2013-07-21 06:45:09
【问题描述】:

我想使用 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 解组器解析这些数据。

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

'entry' 的每个实例都保持为空。当一个条目被编组到 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 {

我该如何解决这个问题?

【问题讨论】:

    标签: java jaxb xsd xjc stax


    【解决方案1】:

    将为您生成一个包含@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;
    

    更多信息

    【讨论】:

    • 谢谢 Blaise,我的 ant 路径中缺少 package-info.java。没想到会编译这个文件。
    • 嗨,我有一个非常相似的问题。你到底需要做什么才能让它发挥作用?我确实有生成的package-info 类,但我不确定如何“使用”它。我是否必须以某种方式将其添加到我的 Maven 构建过程中?
    猜你喜欢
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多