簡體   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