0

I want to validate an XML file like this, which should be the correct way:

File schemaFile = new File("src/main/res/kml21.xsd"); // etc.
Source xmlFileToValidate = new StreamSource(new File(uri.getPath()));
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(xmlFileToValidate);

But I just get this error:

2022-05-30 09:35:04.390 18383-18383/com.example.axistracker E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.axistracker, PID: 18383
    java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
        at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:192)
        at com.example.axistracker.backend.geo.parse.KMLParser.isKMLValid(KMLParser.java:53)

Any idea why I get the "IllegalArgumentException: http://www.w3.org/2001/XMLSchema"?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
patpatwithhat
  • 335
  • 2
  • 11
  • I don’t get the same exception on my jdk-17.0.1.jdk. Does this answer your question? [SchemaFactory doesn't support W3C XML Schema in platform level 8?](https://stackoverflow.com/questions/3129934/schemafactory-doesnt-support-w3c-xml-schema-in-platform-level-8) – Ole V.V. May 30 '22 at 08:20
  • Ok. Thanks for that info. Maybe i have to worry about my setup and not about the code itself. I'm working with targestSdk 31, gradle 7.0.4 and jdk 11.0.10 – patpatwithhat May 30 '22 at 08:37
  • Also swiching the jdk didn't solve it so far. – patpatwithhat May 30 '22 at 08:59

0 Answers0