如何在Java中检查XSD是否有效

时间:2016-08-23 11:10:44

标签: java xsd

如何检查给定文件是否是Java 7中的有效XSD文件(没有Internet连接)?

这不重复。我不想针对XSD检查XML,但检查XSD本身是否有效。

到目前为止我尝试过:

@Slf4j
public class Program {

  /**
   * Sample main method.
   * 
   * @param args
   *          program arguments
   */
  public static void main(String[] args) {
    try {
      log.info("Program has started.");
      DocumentBuilder parser = DocumentBuilderFactory.newInstance()
          .newDocumentBuilder();
      Document document = parser.parse(new File("test.xsd"));
      SchemaFactory factory = SchemaFactory
          .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
      Schema schema = factory.newSchema(new URL(
          "http://www.w3.org/2001/XMLSchema"));
...

      log.info("Program has finished - ok.");
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

问题是:

    即使test.xsd有效,
  1. 也会引发一些奇怪的异常
  2. 从互联网上获取验证架构,但我必须在没有的情况下工作 互联网连接
  3. 例外是:

    org.xml.sax.SAXParseException; systemId: http://www.w3.org/2001/XMLSchema; lineNumber: 7; columnNumber: 20; s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw 'XML Schema'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaDOMParser.characters(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaParsingConfig.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaParsingConfig.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaDOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
    at javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
    at javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
    at o2.xml.core.Program.main(Program.java:39)
    

    问题可能在于指定架构,那么我应该指定什么来检查XSD?其他一些预建常常还是什么?

1 个答案:

答案 0 :(得分:0)

这个(基于this other answer)对我来说似乎可以:

URL schemaFile = new URL("https://www.w3.org/2001/XMLSchema.xsd");
Source xmlFile = new StreamSource(XMLSchemaTest.class.
getResourceAsStream("mySchema.xsd"));
SchemaFactory schemaFactory =   
     SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
     Schema schema = schemaFactory.newSchema(schemaFile);
     Validator validator = schema.newValidator();
     validator.validate(xmlFile);
     System.out.println("is valid");
} catch (SAXException e) {
     System.out.println(xmlFile.getSystemId() + " is NOT valid reason:" + e);
} catch (IOException e) {
     e.printStackTrace();
}

我是否也尝试使用http://www.w3.org/2012/04/XMLSchema.xsd,但这一次在读取主架构时会产生错误