我想使用位于安全 https 站点的架构来验证 XML 文件。如何告诉验证器排除自签名证书或使用 https URL?我有一个名为 test.xml 的文件和一个位于https://localhost:1234/module/testschema.xsd
. 我正在使用与此处相同的代码。如果我使用常规 URL ( http://localhost/module/testschema.xsd
),效果很好。如果我用 https URL 替换,则会收到此错误:
schema_reference.4:未能读取架构文档' https://localhost:1234/module/testschema.xsd ',因为 1) 找不到该文档;2) 文件无法读取;3) 文档的根元素不是<xsd:schema>
.
复制代码:
public boolean validateFile(String xml, String strSchemaLocation)
{
Source xmlFile = null;
try {
URL schemaFile = new URL(strSchemaLocation);
xmlFile = new StreamSource(new File(xml));
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId() + " is valid");
} catch (SAXException e) {
System.out.println(xmlFile.getSystemId() + " is NOT valid");
System.out.println("Reason: " + e.getLocalizedMessage());
return false;
} catch (IOException ioe) {
System.out.println("IOException");
return false;
}
return true;
}