我想使用 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.QUALIFIED
的 elementFormDefault
所有与未指定命名空间参数的 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/