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

Pie*_*rre 5 java stax xsd jaxb xjc

我想使用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
Run Code Online (Sandbox Code Playgroud)

我无法获得一个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();
    (...) 
   }
Run Code Online (Sandbox Code Playgroud)

每个"条目"实例都保持为空.当一个条目被封送到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"/>
Run Code Online (Sandbox Code Playgroud)

我认为这是因为xjc忽略了命名空间.它产生:

@XmlRootElement(name = "entry")
public class Entry {
Run Code Online (Sandbox Code Playgroud)

代替 (?)

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

我怎样才能解决这个问题 ?

bdo*_*han 7

将为您生成package-info包含@XmlSchema注释的类.由于a 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;
Run Code Online (Sandbox Code Playgroud)

欲获得更多信息

  • 嗨,我有一个非常相似的问题。要使它正常工作,您到底需要做什么?我确实有生成的`package-info`类,但是我不确定如何“使用”它。我是否必须以某种方式将其添加到我的Maven构建过程中? (2认同)