如何仅验证 XSD 文件?

问题描述 投票:0回答:1

我想在解析从外部服务器下载的文件之前仅验证 XSD 文件(我不想针对 XSD 架构执行 XML 验证器)。我想确保我正确编写了

validateXsdSchema
函数,其中 SCHEMA_XSD_FILENAME 是 xsd 文件,text 是 xml 文件。 (也许验证功能可以简化?)。

我还有一个问题,总是在

validateXsdSchema
返回 true/false 之后,它仍然显示 throw ReceivedException("Unable to read file", e) 当 xsd 验证失败时,是否可以在该级别抛出异常?

@Component
class ReceivedFile(
    private val xmlParser: XmlParser,
    private val remoteFileOperations: RemoteFileOperations<DirEntry>,
    @Value("\${filePath}") private val path: String
) : ReceivedFileStorage {
    private val SCHEMA_XSD_FILENAME = "file.xsd"


    override fun read(): Verification? {
    var fileVerifcation: FileVerification? = null
        try {
            val files = remoteFileOperations.list(path)
            val fileName = files
                .map { it.filename }
                .firstOrNull { fileName ->
                     remoteFileOperations.get("$path/$fileName") { text ->
                        val contentBytes = content.readBytes()
                        if (validateXsdSchema(contentBytes.inputStream())) {
                            fileVerifcation = xmlParser.parse(contentBytes.inputStream())
                        }
                    }
                }
            if (fileName != null) {
                return Verification(fileName)
            }
            return null
        } catch (e: MessagingException) {
            throw ReceivedException("Cannot read file", e)
        }
    }
}
@Throws(SAXException::class, IOException::class)
    private fun validateXsdSchema(content: InputStream): Boolean {
        try {
            val validator = createXsdSchemaValidator()
            val xmlFile: Source = StreamSource(content)
            validator.validate(xmlFile)
            return true
        } catch (exception: SAXException) {
            return false
        }
    }
 @Throws(SAXException::class)
    private fun createXsdSchemaValidator(): Validator {
        val schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
        val schemaSource: Source = StreamSource(
            javaClass.classLoader
                .getResourceAsStream(SCHEMA_XSD_FILENAME)
        )

        val schema = schemaFactory.newSchema(schemaSource)
        val validator = schema.newValidator()
        return validator
    }
java spring spring-boot kotlin xsd-validation
1个回答
0
投票

您可以尝试的实现:

    public static boolean validate(String xml) {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        try {
            File schemaFile = ResourceUtils.getFile("classpath:<xsd file location>/file.xsd");
            Schema schema = factory.newSchema(schemaFile);
            Validator validator = schema.newValidator();

            Source source = new StreamSource(new StringReader(xml));

            validator.validate(source);
        } catch (IOException | SAXException e) {
            log.error(e.getMessage());
            return false;
        }

        return true;
    }
© www.soinside.com 2019 - 2024. All rights reserved.