After decoupling the XML file, all properties that are attributes in the XML file are NULL (fileDateTime, fileId, etc.)
I really don't understand why, because I have the correct annotations @XmlAttribute(name = "FileDateTime")and@XmlAttribute(name = "FileId")
As you can see, I am not using any namespace (so I do not think about the namespace)!
I am using JDK 1.6, Sax 2.0.1 and XercesImpl 2.9.1
Thank you for your help.
test.xml
<KeyImport_file FileDateTime="2013-05-30T09:00:00" FileId="KeyImport_source_20121231124500">
<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...
Analysis method (non-marshal and XSD verification):
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 analysis method without using sax and it works. Any idea why? I would like to use sax with jabx for a 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);
source
share