开发者

Load schema from the internet

开发者 https://www.devze.com 2023-03-25 17:41 出处:网络
I ne开发者_JAVA百科ed to load a Schema object from the internet, but I don\'t know how to do it. The URL is like https://.../.../schema.xsd.

I ne开发者_JAVA百科ed to load a Schema object from the internet, but I don't know how to do it. The URL is like https://.../.../schema.xsd.

Do you have any hints?


The JavaDoc you linked to mentions that "[a] Schema object is usually created from SchemaFactory."

And SchemaFactory has this nice newSchema method that takes an URL.


Say for instance you want to do XML validation from XSD:

static boolean validateXMLAgainstXSD(String xml, String xsd) {
    try {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(new StreamSource(xsd));
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(xml));
        return true;
    }
    catch(Exception ex) {
        return false;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消