gpt4 book ai didi

validation - JAXB 解码验证抛出 cvc-elt.1 : Cannot find the declaration of element error

转载 作者:行者123 更新时间:2023-12-04 13:04:28 26 4
gpt4 key购买 nike

我是 JAXB 和验证的新手,花了几个小时试图找出这个问题,但无济于事。我创建了一个简单的 JAXB 解码器示例来解析 XML 文件。我也创建了一个合适的 XSD 文件,但验证器一直提示它无法找到元素的声明。

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

我不断收到的错误/异常是:

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

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

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);

计算借款数据响应.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>

计算借款数据响应.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>

我尝试了有和没有 XSD 中的最后一个元素定义(即: xsd:element name="calculateBorrowingDataResponse"... ),但都不起作用。

我已经没有什么可以尝试的不同想法了。任何建议或建议将不胜感激!

最佳答案

这是我试图找出问题根源的第四个小时。经过一番挣扎,现在,我相信您缺少一行代码就可以上升到光辉的高度!

问题是DocumentBuilderFactory通过 DocumentBuilderFactory.newInstance() 创建默认情况下不知道命名空间——是的。

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

  • 让您的 DocumentBuilderFactory命名空间感知:

    DocumentBuilderFactory.setNamespaceAware(true);
  • 或使用 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);

    顺便说一下,在这个模式感知性文档构建器令人敬畏的 Unmarshaller class' documentation 的顶部有很多信息。 ,你一定要检查一下!

    关于validation - JAXB 解码验证抛出 cvc-elt.1 : Cannot find the declaration of element error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8761930/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com