Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB unmarshal return null for attributes

After unmarshalling the XML file, all properties which are attributes in the XML file have the value NULL (fileDateTime, fileId, etc...)

I don't really understand why as I have the correct annotations @XmlAttribute(name = "FileDateTime") and @XmlAttribute(name = "FileId")

As you can see I don't use any namespace (so not a namespace issue I think!)

I am using JDK 1.6, Sax 2.0.1 and XercesImpl 2.9.1

Thanks for your help.

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

The parse method (unmarshal & XSD validation):

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

Just change my parse method without using sax and it works. Any idea why? I would like to use sax with jabx for performance issue.

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);
like image 223
TheEwook Avatar asked Apr 12 '13 11:04

TheEwook


1 Answers

Resolved

It seems that JAXB doesn't work properly with SAX parser unless parser is set to be namespace aware.

Just added this line and it works fine

saxParserFactory.setNamespaceAware(true);
like image 170
TheEwook Avatar answered Sep 27 '22 20:09

TheEwook