1

I would like to validate an XML file using a schema located at a secure https site. How do I tell the validator to except a self-signed certificate or use an https URL? I have a file called test.xml and a schema located at https://localhost:1234/module/testschema.xsd. I'm using the same code found here. If I use a regular URL (http://localhost/module/testschema.xsd), it works great. If I substitute with an https URL, then I get this error:

schema_reference.4: Failed to read schema document 'https://localhost:1234/module/testschema.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

Copied Code:

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;
}
Community
  • 1
  • 1
jmad1
  • 13
  • 1
  • 3

2 Answers2

1

This has very little to to do with schema validation. Your problem is that you need to establish an HTTPS connection and trust a self-signed certificate. See How can I use different certificates on specific connections? or google around for that.

I don't think you'll be able to use the SchemaFactory.newSchema factory method that takes a File, so just use the one that takes a StreamSource:

URL schemaFile = new URL(strSchemaLocation);
HttpsURLConnection schemaConn = (HttpsURLConnection)schemaFile.openConnection();
// Magic from the other answer to accept self-signed cert
InputStream is = schemaConn.getInputStream();
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new StreamSource(is));

(I'm leaving out the try..catch to close the input stream and the connection)

Community
  • 1
  • 1
ykaganovich
  • 14,736
  • 8
  • 59
  • 96
  • Ah yes, I was trying to force the validator to do something it wasn't suppose to do and wasn't thinking outside the box. I got it to work with your suggestion. Thank you. – jmad1 Jan 11 '13 at 04:32
0

It's not a validation problem, java.net.URL supports https, there should be bo difference. Just make sure that you can open https://localhost:1234/module/testschema.xsd with a browser.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275