【问题标题】:Load schema from the internet从 Internet 加载架构
【发布时间】:2011-10-18 21:00:47
【问题描述】:

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

你有什么提示吗?

【问题讨论】:

    标签: java xml validation schema


    【解决方案1】:

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

    SchemaFactorythis nice newSchema method 接受URL

    【讨论】:

      【解决方案2】:

      比如说你想从 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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-18
        • 2013-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-04
        • 2021-03-01
        相关资源
        最近更新 更多