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:
- throws some strange exception even if test.xsd is valid
- 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 ?