从互联网加载架构

时间:2011-08-03 07:23:15

标签: java xml validation schema

我需要从互联网上加载Schema对象,但我不知道该怎么做。该网址类似于https://.../.../schema.xsd

你有任何提示吗?

2 个答案:

答案 0 :(得分:1)

您链接的JavaDoc提到“[a] Schema对象通常是从SchemaFactory创建的。”

SchemaFactory this nice newSchema method URL,{{1}}。

答案 1 :(得分:0)

比如说你想从XSD进行XML验证:

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;
    }
}