2

How can I check if the given file is valid XSD file in Java 7 (without internet connectivity) ?

This is not duplicate. I do not want to check XML against XSD but check if XSD is valid itself.

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
  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 ? Is some other pre-build constant or what ?

kulatamicuda
  • 1,603
  • 2
  • 21
  • 40

1 Answers1

0

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

Edoardo Panfili
  • 322
  • 3
  • 8