2013-04-12 18 views
5

Dopo unmarshalling il file XML, tutte le proprietà che sono gli attributi nel file XML hanno il valore NULL (FileDateTime, fileId, ecc ...)JAXB Unmarshal tornare nullo per gli attributi

Io non capisco perché, come ho le annotazioni corrette @XmlAttribute(name = "FileDateTime") e @XmlAttribute(name = "FileId")

Come potete vedere non uso alcun namespace (quindi non è un problema di spazio dei nomi credo!)

sto usando JDK 1.6, Sax 2.0.1 e 2.9 XercesImpl .1

Grazie per il tuo aiuto.

test.xml

<KeyImport_file FileDateTime="2013-05-30T09:00:00" FileId="KeyImport_source_20121231124500"> 
    <!--1 or more repetitions:--> 
    <record record_number="10"> 
    ... 
    </record> 
</KeyImport_file> 

KeyImportFile.java

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "record" 
}) 
@XmlRootElement(name = "KeyImport_file") 
public class KeyImportFile { 

    @XmlElement(required = true) 
    protected List<KeyImportFile.Record> record; 

    @XmlAttribute(name = "FileDateTime") 
    @XmlSchemaType(name = "dateTime") 
    protected XMLGregorianCalendar fileDateTime; 

    @XmlAttribute(name = "FileId") 
    protected String fileId; 
etc... 
etc... 

Il metodo parse (unmarshal convalida & XSD):

import org.xml.sax.InputSource; 
import org.xml.sax.XMLReader; 

import javax.xml.XMLConstants; 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.UnmarshallerHandler; 
import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 
import javax.xml.transform.stream.StreamSource; 
import javax.xml.validation.Schema; 
import javax.xml.validation.SchemaFactory; 
import java.io.InputStream; 

private KeyImportFile parseXML(final InputStream xmlInputStream, final StreamSource xsdSource) 
     throws Exception 
{ 
    KeyImportFile keyImportFile; 

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
    Schema schema = schemaFactory.newSchema(xsdSource); 

    JAXBContext jc = JAXBContext.newInstance(KeyImportFile.class); 

    UnmarshallerHandler unmarshallerHandler = jc.createUnmarshaller().getUnmarshallerHandler(); 

    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 
    saxParserFactory.setSchema(schema); 

    SAXParser saxParser = saxParserFactory.newSAXParser(); 
    XMLReader xmlReader = saxParser.getXMLReader(); 
    xmlReader.setContentHandler(unmarshallerHandler); 
    xmlReader.setErrorHandler(keyImportErrorHandler); 

    InputSource inputSource = new InputSource(xmlInputStream); 
    xmlReader.parse(inputSource); 
    xmlInputStream.close(); 

    keyImportFile = (KeyImportFile) unmarshallerHandler.getResult(); 

    return keyImportFile; 
} 

EDIT

Basta cambiare il mio metodo di analisi senza utilizzare il sassofono e funziona. Qualche idea del perché? Mi piacerebbe usare sax con jabx per problemi di prestazioni.

KeyImportFile keyImportFile; 

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
Schema schema = schemaFactory.newSchema(xsdSource); 

JAXBContext jc = JAXBContext.newInstance(KeyImportFile.class); 

Unmarshaller unmarshaller = jc.createUnmarshaller(); 
unmarshaller.setSchema(schema); 
keyImportFile = (KeyImportFile) unmarshaller.unmarshal(xmlInputStream); 
+1

Se siete interessati in termini di prestazioni, allora vorrei andare con l'approccio che ci ha fornito nella tua modifica. Non solo è meno codice, ma ti impedisce di compromettere le ottimizzazioni delle prestazioni messe in atto dal provider JAXB. Ad esempio EclipseLink JAXB (MOXy) utilizza StAX come parser sottostante (StAX è in genere più veloce di SAX) e il codice renderebbe MOXy più lento anziché più veloce. –

risposta

7

risolto

Sembra che JAXB non funziona correttamente con SAX parser meno che parser è impostato per essere consapevoli dello spazio dei nomi.

appena aggiunto questa linea e funziona benissimo

saxParserFactory.setNamespaceAware(true); 
Problemi correlati