简体   繁体   English

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

[英]How to check in Java that XSD is valid

How can I check if the given file is valid XSD file in Java 7 (without internet connectivity) ? 如何检查给定文件是否是Java 7中的有效XSD文件(没有Internet连接)?

This is not duplicate. 这不重复。 I do not want to check XML against XSD but check if XSD is valid itself. 我不想针对XSD检查XML,但检查XSD本身是否有效。

What I have tried so far: 到目前为止我尝试过的:

@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();
    }
  }
}

The problem is: 问题是:

  1. throws some strange exception even if test.xsd is valid 即使test.xsd有效,也会抛出一些奇怪的异常
  2. fetching validation schema from internet, but I have to work without internet connection 从互联网上获取验证架构,但我必须在没有互联网连接的情况下工作

The exception is: 例外是:

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)

Problem is probably in specifying of schema, so what should I specify to check XSD ? 问题可能在于指定模式,那么我应该指定什么来检查XSD? Is some other pre-build constant or what ? 其他一些预建常常还是什么?

This one (based on this other answer ) seems ok for me: 这个(基于这个其他答案 )对我来说似乎没问题:

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

I did I try also using http://www.w3.org/2012/04/XMLSchema.xsd but this one generate an error during the main schema reading 我也尝试使用http://www.w3.org/2012/04/XMLSchema.xsd,但这个在主模式读取期间生成错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM