Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB unmarshal validation throws cvc-elt.1: Cannot find the declaration of element error
    primarykey
    data
    text
    <p>I'm kind of new to JAXB and validation, and have spent several hours trying to figure out this problem to no avail. I've created a simple JAXB unmarshaller sample to parse an XML file. I have created an appropriate XSD file as well, but the validator keeps complaining that it is unable to find the declaration of an element.</p> <p>I think it may be related to namespace issues, but I've tried everything I can think of and still can't seem to resolve the error. As far as I can tell, my XSD and XML are proper, so it may have to do with the way I am instantiating the unmarshaller, but I can't seem to find the problem anywhere.</p> <p>The error/exception I keep getting is:</p> <pre><code>Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'calculateBorrowingDataResponse'. at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source) at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source) at org.apache.xerces.jaxp.validation.ValidatorHandlerImpl.startElement(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:85) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:47) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:113) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:236) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:119) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:102) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:299) ... 2 more </code></pre> <p>Here are the source files that are causing the error.</p> <p>Java Code:</p> <pre><code>// We need a Document InputStream is = UnmarshalTest.class.getClassLoader().getResourceAsStream("calculateBorrowingDataResponse.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Node node = db.parse(is); // Creating an unmarshaller Unmarshaller u = JAXBContext.newInstance(CalculateBorrowingDataResponseType.class).createUnmarshaller(); // Setting the Validation Schema schema; SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schema = schemaFactory.newSchema(new File("src/main/webapp/WEB-INF/wsdl/CalculateBorrowingDataResponse.xsd")); u.setSchema(schema); u.unmarshal(node, CalculateBorrowingDataResponseType.class); </code></pre> <p>CalculateBorrowingDataResponse.xsd:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsd:schema version="1.1" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.domain.com/ClientServices/LendingSimulation/CalculateBorrowingDataResponse" xmlns:lssSt="http://www.domain.com/ClientServices/LendingSimulation/CalculateBorrowingDataResponse" xmlns:cbdRes="http://www.domain.com/ClientServices/LendingSimulation/CalculateBorrowingDataResponse" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;!-- CalculateBorrowingData --&gt; &lt;xsd:complexType name="CalculateBorrowingDataResponseType"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="loanAgmt" type="cbdRes:LoanAgreementType" minOccurs="1" maxOccurs="1" /&gt; &lt;/xsd:sequence&gt; &lt;/xsd:complexType&gt; &lt;xsd:complexType name="LoanAgreementType"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="borrowingBasedPmtAmt" type="lssSt:borrowingBasedPmtAmt" minOccurs="0" maxOccurs="1" /&gt; &lt;xsd:element name="maxPmtAmt" type="lssSt:maxPmtAmt" minOccurs="0" maxOccurs="1" /&gt; &lt;xsd:element name="borrowingCapacityMin" type="lssSt:borrowingCapacityMin" minOccurs="0" maxOccurs="1" /&gt; &lt;xsd:element name="borrowingCapacityMax" type="lssSt:borrowingCapacityMax" minOccurs="0" maxOccurs="1" /&gt; &lt;xsd:element name="propertyValueMinAmt" type="lssSt:propertyValueMinAmt" minOccurs="0" maxOccurs="1" /&gt; &lt;xsd:element name="propertyValueMaxAmt" type="lssSt:propertyValueMaxAmt" minOccurs="0" maxOccurs="1" /&gt; &lt;/xsd:sequence&gt; &lt;/xsd:complexType&gt; &lt;xsd:element name="calculateBorrowingDataResponse" type="cbdRes:CalculateBorrowingDataResponseType"/&gt; &lt;xsd:simpleType name="borrowingBasedPmtAmt"&gt; &lt;xsd:restriction base="xsd:decimal" &gt; &lt;xsd:totalDigits value="19" /&gt; &lt;xsd:fractionDigits value="4" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;xsd:simpleType name="maxPmtAmt"&gt; &lt;xsd:restriction base="xsd:decimal" &gt; &lt;xsd:totalDigits value="19" /&gt; &lt;xsd:fractionDigits value="4" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;xsd:simpleType name="borrowingCapacityMin"&gt; &lt;xsd:restriction base="xsd:decimal" &gt; &lt;xsd:totalDigits value="19" /&gt; &lt;xsd:fractionDigits value="4" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;xsd:simpleType name="borrowingCapacityMax"&gt; &lt;xsd:restriction base="xsd:decimal" &gt; &lt;xsd:totalDigits value="19" /&gt; &lt;xsd:fractionDigits value="4" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;xsd:simpleType name="propertyValueMinAmt"&gt; &lt;xsd:restriction base="xsd:decimal" &gt; &lt;xsd:totalDigits value="19" /&gt; &lt;xsd:fractionDigits value="4" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;xsd:simpleType name="propertyValueMaxAmt"&gt; &lt;xsd:restriction base="xsd:decimal" &gt; &lt;xsd:totalDigits value="19" /&gt; &lt;xsd:fractionDigits value="4" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;/xsd:schema&gt; </code></pre> <p>calculateBorrowingDataResponse.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;calculateBorrowingDataResponse xmlns="http://www.domain.com/ClientServices/LendingSimulation/CalculateBorrowingDataResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://www.domain.com/ClientServices/LendingSimulation/V1.1"&gt; &lt;loanAgmt&gt; &lt;borrowingBasedPmtAmt&gt;1231231&lt;/borrowingBasedPmtAmt&gt; &lt;maxPmtAmt&gt;987654321&lt;/maxPmtAmt&gt; &lt;borrowingCapacityMax&gt;99999&lt;/borrowingCapacityMax&gt; &lt;/loanAgmt&gt; &lt;/calculateBorrowingDataResponse&gt; </code></pre> <p>I tried both with and without the last element definition in the XSD (ie: xsd:element name="calculateBorrowingDataResponse" ... ) but neither work. </p> <p>I'm running out of ideas of different things to try. Any suggestions or recommendations would be greatly appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload