3
votes

I have the root element in my xml file as follows:

<Document xmlns="urn:kad:ns:file:1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

and the XSD header is as follows:

 <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" 
targetNamespace="urn:kad:ns:file:1" xmlns:xs="http://www.w3.org/2001/XMLSchema">

When I try to unmarshal the file :

JAXBContext jc = JAXBContext.newInstance("path.to.package.of.schema.objects");
Unmarshaller u = jc.createUnmarshaller();

// read the file stream and unmarshall the XML
File f = new File(xmlFilePath);
Document document = (Document)u.unmarshal(f);

am getting the following exception:

Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.1.3.v20110304-r9073): org.eclipse.persistence.exceptions.XMLMarshalException Exception Description: A descriptor with default root element {urn:kad:ns:file:1}Document was not found in the project

Is this something specific for having a urn as the target namespace in the XSD or am I missing something in the JAVA file unmarshalling the XML file?

Edit

When I remove the default namespace declaration form the xml file xmlns="urn:kad:ns:file:1", the unmarshal process works fine. But no validation occurs, meaning if I remove a required element from the XML, the process will continue at JAVA level and consider this element mapped object as null.

I was wondering what affect does this (the default namespace attribute in root element) have on the unmarshalling process? Does it instantly validate the XML against the XSD at the URN am specifying (which I suspect it is not finding in my case) ?

How can I validate the XML when unmarshalling agaisnt the XSD I have?

1
See This question to see if it helps you.pringi
Does your schema actually have a global Document element?lexicore
@lexicore: yes it does,<xs:element name="Document">...</xs:element>, but am suspecting that when jaxb is unmarshalling, it is not finding the schema and considering it as not in project.. Is this ture? Please check the editKAD
Do you generate your code or write/annotate classes manually? Do you have an ObjectFactory in your package? JAXB does not consider schema during marshalling/unmarshalling (only for validation, if configured). It only uses your annotated classes. There should be an @XmlRootElement annotation somewhere or an ObjectFactory with @XmlElementDecl.lexicore
The classes have been generated automatically, objectFactory exists in the package, as well as a Document.java class which is the mapping of the schema with the following annotation @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "chargelog" }) @XmlRootElement(name = "Document").KAD

1 Answers

3
votes

The problem was within the package-info.java class.

The package annotated with the XMLSchema annotation that has the default namespace set to urn:kad:ns:file:1, was not the same one used within the Root mapped object of the XML (Document).

So when the JAXB tries to unmarshall the XML, it cannot find the (Document) element in the package namespace since there is no namespace defined in the first place.

Document.java

package path.to.pkg.jaxbobjects;
....

package-info.java

@javax.xml.bind.annotation.XmlSchema(namespace =
                                     "urn:kad:ns:file:1",
                                     elementFormDefault =
                                     javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package path.to.pkg.jaxbobjects2;

When removing the default namespace, this action is skipped and the file is allowed to be unmarshalled.