JAXB unmarshal验证抛出cvc-elt.1:找不到元素错误的声明

Eri*_* B. 6 validation jaxb unmarshalling

我对JAXB和验证都不熟悉,并花了几个小时试图弄清楚这个问题无济于事.我创建了一个简单的JAXB unmarshaller示例来解析XML文件.我也创建了一个合适的XSD文件,但验证器一直在抱怨它无法找到元素的声明.

我认为它可能与命名空间问题有关,但我已经尝试了我能想到的一切,但似乎仍无法解决错误.据我所知,我的XSD和XML是正确的,所以它可能与我实例化unmarshaller的方式有关,但我似乎无法在任何地方找到问题.

我一直得到的错误/异常是:

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
Run Code Online (Sandbox Code Playgroud)

以下是导致错误的源文件.

Java代码:

// 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);
Run Code Online (Sandbox Code Playgroud)

CalculateBorrowingDataResponse.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<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">


    <!-- CalculateBorrowingData -->
    <xsd:complexType name="CalculateBorrowingDataResponseType">
        <xsd:sequence>
            <xsd:element name="loanAgmt" type="cbdRes:LoanAgreementType" minOccurs="1" maxOccurs="1" />
        </xsd:sequence>
    </xsd:complexType>


    <xsd:complexType name="LoanAgreementType">
        <xsd:sequence>
            <xsd:element name="borrowingBasedPmtAmt" type="lssSt:borrowingBasedPmtAmt" minOccurs="0" maxOccurs="1" />
            <xsd:element name="maxPmtAmt" type="lssSt:maxPmtAmt" minOccurs="0" maxOccurs="1" />
            <xsd:element name="borrowingCapacityMin" type="lssSt:borrowingCapacityMin" minOccurs="0" maxOccurs="1" />
            <xsd:element name="borrowingCapacityMax" type="lssSt:borrowingCapacityMax" minOccurs="0" maxOccurs="1" />
            <xsd:element name="propertyValueMinAmt" type="lssSt:propertyValueMinAmt" minOccurs="0" maxOccurs="1" />
            <xsd:element name="propertyValueMaxAmt" type="lssSt:propertyValueMaxAmt" minOccurs="0" maxOccurs="1" />
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="calculateBorrowingDataResponse" type="cbdRes:CalculateBorrowingDataResponseType"/>


    <xsd:simpleType name="borrowingBasedPmtAmt">
        <xsd:restriction base="xsd:decimal" >
        <xsd:totalDigits value="19" />
        <xsd:fractionDigits value="4" />
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="maxPmtAmt">
        <xsd:restriction base="xsd:decimal" >
        <xsd:totalDigits value="19" />
        <xsd:fractionDigits value="4" />
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="borrowingCapacityMin">
        <xsd:restriction base="xsd:decimal" >
        <xsd:totalDigits value="19" />
        <xsd:fractionDigits value="4" />
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="borrowingCapacityMax">
        <xsd:restriction base="xsd:decimal" >
        <xsd:totalDigits value="19" />
        <xsd:fractionDigits value="4" />
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="propertyValueMinAmt">
        <xsd:restriction base="xsd:decimal" >
        <xsd:totalDigits value="19" />
        <xsd:fractionDigits value="4" />
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="propertyValueMaxAmt">
        <xsd:restriction base="xsd:decimal" >
        <xsd:totalDigits value="19" />
        <xsd:fractionDigits value="4" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)

calculateBorrowingDataResponse.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <loanAgmt>
        <borrowingBasedPmtAmt>1231231</borrowingBasedPmtAmt>
        <maxPmtAmt>987654321</maxPmtAmt>
        <borrowingCapacityMax>99999</borrowingCapacityMax>
    </loanAgmt>
</calculateBorrowingDataResponse>
Run Code Online (Sandbox Code Playgroud)

我在XSD中尝试使用和不使用最后一个元素定义(即:xsd:element name ="calculateBorrowingDataResponse"......)但是都不起作用.

我尝试了不同的想法.任何建议或建议将不胜感激!

Koh*_*ert 24

这是我试图找到问题根源的第四个小时.经过多次努力,现在,我相信你错过了一行代码才能达到辉煌的高度!

问题是默认情况下DocumentBuilderFactory创建的通道不是名称空间感知 - 是的.DocumentBuilderFactory.newInstance()

您可以通过两种方式克服这个问题:

  1. 让你的DocumentBuilderFactory命名空间知道:

    DocumentBuilderFactory.setNamespaceAware(true);

  2. 或者使用一段StreamSource时间解组并DocumentBuilder完全放弃他和他的小朋友:

    Unmarshaller.unmarshal(StreamSource, Class<T>);

如果是第二种选择,你就是这样做的.

InputStream xsdStream = ...
InputStream xmlStream = ...

SchemaFactory f = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema s = schemaFactory.newSchema(xsdStream);

JAXBContext c = JAXBContext.newInstance(CalculateBorrowingDataResponseType.class);
Unmarshaller u = c.createUnmarshaller();
u.setSchema(schema);
CalculateBorrowingDataResponseType b = 
  u.unmarshal(new StreamSource(xmlStream), CalculateBorrowingDataResponseType.class);
Run Code Online (Sandbox Code Playgroud)

顺便说一句,在这个架构感知 - 文档构建器 - 真棒上,在类的文档的顶部有很多信息,你一定要检查出来!Unmarshaller