2013-07-22 68 views
4

我想分析使用JAXB爲以下XSD架構http://www.uniprot.org/support/docs/uniprot.xsd數據。XJC不會使用命名空間生成@XmlElement?

對於一個典型的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'的每個實例都保持爲空。當條目被編組到標準錯誤,我得到的是這樣的:

<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 { 

我怎麼能解決這個問題(?)?會爲你生成

回答

6

package-info含有@XmlSchema註釋類。由於namespace用與XML元素elementFormDefault等於XmlNsForm.QUALIFIED所有批註沒有指定將屬於這個命名空間的命名空間參數指定一起。

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

更多信息

+1

謝謝布萊斯,package-info.java失蹤INT我的螞蟻路徑。我沒想到這個文件會被編譯。 – Pierre

+1

嗨,我有一個非常類似的問題。你究竟做了什麼才能使它工作?我確實有生成的'package-info'類,但我不確定如何「使用」它。我必須將它添加到我的Maven構建過程中嗎? – L42