繁体   English   中英

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

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

如何检查给定文件是否是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();
    }
  }
}

问题是:

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

例外是:

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? 其他一些预建常常还是什么?

这个(基于这个其他答案 )对我来说似乎没问题:

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,但这个在主模式读取期间生成错误

暂无
暂无

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

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