开发者

Load schema from the internet

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜