17

I maintain the JDOM project and I am trying to 'certify' it on Android. Most things work fine, but XML Schema validation is proving problematic...

My questions are: Is there a way to do XMLSchema validation on Android? If there is, how?

Questions like this have been asked before, but no conclusive answer is given:

This is what I currently 'know' (correct me if I am wrong)...:

  • SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema") - fails with IllegalArgumentException
  • the default 'ExpatParser' in Android from the Harmony project does not support XMLSchema validation.
  • It is possible to load Xerces in an Android app, and even parse with it, but not to do a Validating parse - when you try you get 'FATAL' Android exceptions causing application crashes.

I have been working on this for some time now, and I have put together the following 'research':

If anyone has any more information about XMLSchema Validation on Android I would greatly appreciate any input at all.

If anyone has successfully done XMLSchema validation on XML and can help me get the functionality working for JDOM they'll get thousands of internet points ... ;-) and will be immortalized in the JDOM code and commit messages.

Community
  • 1
  • 1
rolfl
  • 17,539
  • 7
  • 42
  • 76

3 Answers3

2

I'm fascinated that, after several years, this still is an open issue. There only seems to be bad news, though. According to the AOSP Issue Tracker Schema validation currently seems unsupported with the standard Android APIs and Google seems to be unwilling to fix it:

Our XML APIs (including SAX, XmlPull and DOM) don't support any of the following:

 XML Schema 

 XML DTDs (including external entity declarations and references)
 validation

 element content whitespace

However, one commenter of that same ticket references a workaround, and provides example code using a Xerces port. I don't know if this goes beyond what you've figured out, so far, but I hope it helps.

To wrap this up: The SchemaFactoryFinder only knows the following Schema definitions:

Using any other schema definition causes it to fail (It will however log this on debug level). Obviously this is the case for you, as you're using another reference to the 2011 schema. So "correctly" referencing the Schema Definition should fix this issue.

juwi
  • 389
  • 3
  • 16
  • 1
    Interesting update, I am trying to digest what you have there.... it appears to be just a link to a crypto library that internally rebuilds a complete Xerces parser as a dependency. This is a predicament, because it requires pulling a subset of an existing project, and would require me to maintain the full parser as well. This is an interesting lead, but not a solution, at least as far as I can tell. – rolfl Sep 27 '14 at 17:31
  • Yeah, I figured. Also double checked with our code base. We do some XML parsing here and there, but no validation takes place, anywhere. I cannot really wrap my head around it, as of yet. Going to check for the actual implementation in libcore and see whats there – juwi Sep 27 '14 at 17:42
  • Actually... I think I found the Problem: https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/javax/xml/validation/SchemaFactoryFinder.java Line 237ff. – juwi Sep 27 '14 at 17:56
  • I edited the answer to reflect the findings I made in the Android Source. Let me know if this resolves the issue. – juwi Sep 27 '14 at 19:10
0

I am using

javax.xml.parsers.DocumentBuilder;
javax.xml.parsers.DocumentBuilderFactory;
javax.xml.parsers.ParserConfigurationException;

If you got a XML string you can parse it by:

Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);

please don't forget the exception handling.

Ben
  • 696
  • 9
  • 19
  • 1
    The op is explicitly asking for validation against a schema, not just mere parsing! – juwi Sep 27 '14 at 16:51
-1

I think you could use RelaxNG - there are plenty of validators available. http://relaxng.org/#validators

Of particular interest should be - http://www.kohsuke.org/relaxng/bali/doc/ - https://msv.java.net/ - http://www.davidashen.net/rnv.html

The latter is C implementation, the first two are written in Java.

If you need high performance, you could write some JNI code and call functions in the rnv source. A simpler approach would be to just build rnv for Android, using NDK and then call it's executable with parameters.

Something like

Process p = Runtime.exec("/path/to/rnv/exec", [valdidationDoc: String, some more params]);
OutputStream out = p.getOutputStream(); // connected to STDIN of p
InputStream in = p.getInputStream(); // connected to STDOUT of p
out.write(new FileInputStream("/path/to/xml"));

/// read in for succes/errors
fabian
  • 642
  • 6
  • 18
  • Thanks for taking a look. I asked explicitly for XMLSchema validation. Suggesting RelaxNG is not going to help me validate against XMLSchemas. – rolfl Feb 17 '14 at 21:03
  • Bad Day? No, not a bad day. Thanks for asking. I think you are missing the implications of the first sentence in my question. JDOM is a java-only library, and cannot introduce non-java dependencies. Suggesting JNI or external-to-Java solutions is not helpful. – rolfl Feb 17 '14 at 21:31
  • I see, So what does 'certify' mean? – fabian Feb 17 '14 at 21:34
  • Means I can assure the JDOM users that the functionality will work if they use JDOM, and that it follows the same patterns/expectations that JDOM users have (and that I can support/fix if it does not). – rolfl Feb 17 '14 at 21:37