Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Descriptor with default root element was not found in the project jaxb unmarshall

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?

like image 674
KAD Avatar asked Nov 28 '25 05:11

KAD


1 Answers

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.

like image 117
KAD Avatar answered Nov 30 '25 19:11

KAD