针对XSD的SOAP响应获取org.xml.sax.SAXParseException

时间:2018-09-20 15:07:07

标签: java xml xsd soapui

我正在尝试使用Java验证针对xsd模式文件的soap响应。但是在运行时,我遇到了异常

  

org.xml.sax.SAXParseException:src-resolve:无法将名称“ xh:ResponseHeader”解析为一个(n)“类型定义”组件

问题在这里。在我的XSD文件中,我正在使用相对路径调用其他文件。我怀疑我的代码无法读取这些文件。但是相同的代码正在SOAPUI中工作。

XSD;

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xsd:import namespace="http://response.header.emtservice.acxsys.com/ws/schema/3/4/1" schemaLocation="../ResponseHeader.xsd" /> 
<xsd:import namespace="http://common.emtservice.acxsys.com/ws/schema/3/4/1" schemaLocation="../Elements.xsd" />

<xsd:element name="SendTransferResponse">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="responseHeader" type="xh:ResponseHeader" />
            <xsd:element name="transferReferenceNumber" type="t:transferreferencenumber" />
            <xsd:element name="fraudCheckResult" type="t:FraudCheckResult" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

Java代码是

          SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

          FileInputStream fileInputStream = new FileInputStream(new File("Response.xsd"));  

          Schema schema = factory.newSchema(new StreamSource(fileInputStream));  


          Validator val = schema.newValidator(); 

          val.validate(new StreamSource(soapResponse));

谢谢

1 个答案:

答案 0 :(得分:0)

下面的代码运行正常

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(new File("Response.xsd"));

    Validator val = schema.newValidator();

    // create a source from a string
    Source source = new StreamSource(new StringReader(response));

    // check input
    boolean isValid = true;
    try {

        val.validate(source);
    } catch (SAXException e) {

        System.err.println(e);
        System.err.println("Not valid");
        isValid = false;
    }

    return isValid;

}